我想通过 _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"
Run Code Online (Sandbox Code Playgroud)
这是我来自客户端的 ajax 调用。
$.ajax({
type: 'PUT',
data: updatedUser,
url: '/users/updateuser/' + globalUserID,
dataType: 'JSON'
}).done(function(response){
Run Code Online (Sandbox Code Playgroud)
这是路由代码。
/*
* 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( …Run Code Online (Sandbox Code Playgroud)