I am trying to create an application using MongoDB, Mongoose, JQuery and Node.js. Whenever I try to add to an array in my schema, sometimes it will add this element several times.
Scheme
This is my scheme, I'm trying to add my upvote and downvote arrays that track users who voted by their id.
var SuggestionSchema = new mongoose.Schema ({
title: String,
content: String,
link: String,
upvote: [{
user_id: {
type: Schema.ObjectId,
ref: 'User'
}
}],
downvote: [{
user_id: {
type: Schema.ObjectId,
ref: 'User'
}
}],
user_id: {
type: Schema.ObjectId,
ref: 'User'
},
category_id: {
type: Schema.ObjectId,
ref: 'Category'
},
});
Route and request
Here is my route and request
router.put('/:suggestion_id/downvote', function(req, res, next) {
Suggestion.findByIdAndUpdate(
req.params.suggestion_id,
{$push: {"downvote": req.body}},
function(err, suggestion) {
res.json(suggestion);
})
});
Ajax Call JQuery
PUT, .
$('#downvote').click(function(){
var user = {
user_id: current_user._id
}
$.ajax({
url: current_url + "suggestions/" + current_suggestion + '/downvote',
type: 'PUT',
data: user,
success: function(data){
//callback
}
});
}
, :
:
: net:: ERR_EMPTY_RESPONSE
, 2 3 downvote . , , , , , . , - . 6 , . !