【发布时间】:2015-02-06 22:39:42
【问题描述】:
我想通过 _id 在 mongoDB 的集合中更新一条记录。
更新:我将 res 更改为 req(谢谢!)并在我传入的 objectId 周围实现了 db.ObjectId(),现在我收到 500 内部服务器错误。
"_id" : ObjectId("54d5296711436278137af74b"),
"username" : "alex",
"email" : "alex@gmail",
"fullname" : "alex man",
"age" : "15",
"location" : "minneap",
"gender" : "mal"
这是我来自客户端的 ajax 调用。
$.ajax({
type: 'PUT',
data: updatedUser,
url: '/users/updateuser/' + globalUserID,
dataType: 'JSON'
}).done(function(response){
这是路由代码。
/*
* 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}
);
});
});
我收到 200 回复,但我的记录没有更新。我的语法有什么问题?
【问题讨论】:
-
res.send 将始终发送 200。因为它返回 '' 或状态码为 200 OK 的错误。如果你有错误,你应该做 res.status(500).send('Something broken!');
标签: javascript node.js mongodb express