小编jav*_*ins的帖子

使用express和node.js更新mongodb中的单个记录

我想通过 _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)

javascript mongodb node.js express

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

express ×1

javascript ×1

mongodb ×1

node.js ×1