I want to update a single record by _id in a collection in mongoDB.
UPDATE: I changed the res to req (thanks!) and implemented the db.ObjectId() around the objectId I am passing in and now I get a 500 internal server error.
"_id" : ObjectId("54d5296711436278137af74b"),
"username" : "alex",
"email" : "alex@gmail",
"fullname" : "alex man",
"age" : "15",
"location" : "minneap",
"gender" : "mal"
This is my ajax call from the client.
$.ajax({
type: 'PUT',
data: updatedUser,
url: '/users/updateuser/' + globalUserID,
dataType: 'JSON'
}).done(function(response){
This is the routing code.
/*
* PUT to updateuser
*/
router.put('/updateuser/:id', function(req, res) {
var db = req.db;
var userToUpdate = req.params.id;
db.collection('userlist').update(
{ _id: userToUpdate},
req.body,
function(err, result){
res.send(
(err === null) ? {msg: ''} : {msg: err}
);
});
});
I get a 200 response back but my record is not updated. What is wrong with my syntax?