无法使用express + mongo + node更新基于id的数据

时间:2017-04-30 11:45:50

标签: node.js mongodb express objectid

我是node / express / mongo的新手。我正在努力学习但是一个错误正在完成我的一天。不知道为什么。谷歌搜索它仍然找不到任何适当的解决方案。以下是来自pastebin的代码,您可以在其中找到我的代码。

https://pastebin.com/kZLHAA5k

我发生了以下错误,虽然我使用objectID for mongo从url获取id参数。你能帮帮我吗...

Error: 
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (c:\Users\AD LORD\Desktop\learning-node\node_modules\bson\lib\bson\objectid.js:50:11)
at ObjectID (c:\Users\AD LORD\Desktop\learning-node\node_modules\bson\lib\bson\objectid.js:31:42)
at app.get (c:\Users\AD LORD\Desktop\learning-node\server.js:35:44)
at Layer.handle [as handle_request] (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\layer.js:95:5)
at next (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\layer.js:95:5)
at c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\index.js:281:22
at param (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\index.js:354:14)
at param (c:\Users\AD LORD\Desktop\learning-node\node_modules\express\lib\router\index.js:365:14)

1 个答案:

答案 0 :(得分:0)

由于我没有得到任何答案,我设法解决了这个问题。似乎我的参数没有正确设置错误来了,所以我更新了我的路线,如下所示:

// update quote
app.post( '/update/edits', ( req, res ) => {
    db.collection('quote').update (
        { _id: ObjectId(req.body.quoteID) },
        { $set: {
            name: req.body.name,
            quote: req.body.quote
            }
        }, function (err, result) {
            if (err) {
                req.flash('error', err);
            } else {
                req.flash('success', 'Quote updated successfully');
            }
            res.redirect('/');
     });
});

顺便说一下,如果它对任何其他人有帮助,你可以在我的github中检查完成的CRUD系统。

Simple Quotes - Basic CRUD System