gpt4 book ai didi

node.js - 类型错误 : Cannot read property '_id' of undefined

转载 作者:IT老高 更新时间:2023-10-28 13:13:41 25 4
gpt4 key购买 nike

我收到错误“TypeError: Cannot read property '_id' of undefined”,这是一个简单的发布请求,将文档保存到名为 books 的集合中,我的有效载荷如下所示:

{
"name": "practical view",
"author": "DN",
"location": "room 50"
}

我只是在我的 express route 执行 db.books.save()。由于我没有传递 id,因此理想情况下应该可以,但在这种情况下不行。

以下是我在 Node 服务器上获得的完整错误转储:

C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\mongo_client.js:411
throw err
^
TypeError: Cannot read property '_id' of undefined
at Collection.save (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\collection.js:393:15)
at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\index.js:203:65
at apply (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:16:28)
at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:20:25
at Db.collection (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\db.js:488:44)
at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\index.js:268:7
at apply (C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:16:28)
at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\thunky\index.js:20:25
at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\index.js:300:4
at C:\NodeProjects\ExpressTutorial\Library\node_modules\mongojs\node_modules\mongodb\lib\mongodb\mongo_client.js:408:11
31 Aug 00:14:30 - [nodemon] app crashed - waiting for file changes before starting...

最佳答案

确保 npm install body-parser,然后添加

var bodyParser = require('body-parser');

app.use(bodyParser());

到代码的顶部。这也是假设您使用的是 Express。

关于node.js - 类型错误 : Cannot read property '_id' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18542329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com