I am getting error "TypeError: Cannot read property '_id' of undefined" on a simple post request to save a document to the collection called books
, My payload looks like this:
我收到错误“TypeError:无法读取未定义的属性'_id'”在一个简单的帖子请求上将文档保存到名为books的集合中,我的有效负载如下所示:
{
"name": "practical view",
"author": "DN",
"location": "room 50"
}
And I am just doing db.books.save()
in my route in express. Since I am not passing the id, this should ideally work, but not in this case.
我只是在我的快递路线上做db.books.save()。由于我没有传递id,理想情况下这应该可行,但在这种情况下不行。
Below is the full error dump I am getting on my node server:
下面是我在节点服务器上获得的完整错误转储:
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...
4 个解决方案
#1
8
Make sure to npm install body-parser, then add
确保npm install body-parser,然后添加
var bodyParser = require('body-parser');
var bodyParser = require('body-parser');
app.use(bodyParser());
app.use(bodyParser());
to the top of your code. This is also assuming you are using Express.
到你的代码顶部。这也假设您正在使用Express。
#2
4
Please try this:
请试试这个:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
#3
0
Even though there is an answer for this question, another solution is to check the version of your express js in the node.json. In my own case, I switched the version from 3.x to 4.x and that was enough to get rid of this error. I hope it helps someone.
即使有这个问题的答案,另一个解决方案是检查node.json中的快速js的版本。在我自己的情况下,我将版本从3.x切换到4.x,这足以摆脱这个错误。我希望它对某人有帮助。
#4
0
This is due to the version of dependencies you are using in package.json, if you are referring any blog or tutorial go and download their code and see what version of dependencies they are using and install that dependencies in your project. Hope you solve your error, HAPPY CODING. :)
这是由于您在package.json中使用的依赖项版本,如果您引用任何博客或教程,请下载其代码并查看它们使用的依赖项版本,并在项目中安装该依赖项。希望你解决你的错误,快乐编码。 :)
#1
8
Make sure to npm install body-parser, then add
确保npm install body-parser,然后添加
var bodyParser = require('body-parser');
var bodyParser = require('body-parser');
app.use(bodyParser());
app.use(bodyParser());
to the top of your code. This is also assuming you are using Express.
到你的代码顶部。这也假设您正在使用Express。
#2
4
Please try this:
请试试这个:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
#3
0
Even though there is an answer for this question, another solution is to check the version of your express js in the node.json. In my own case, I switched the version from 3.x to 4.x and that was enough to get rid of this error. I hope it helps someone.
即使有这个问题的答案,另一个解决方案是检查node.json中的快速js的版本。在我自己的情况下,我将版本从3.x切换到4.x,这足以摆脱这个错误。我希望它对某人有帮助。
#4
0
This is due to the version of dependencies you are using in package.json, if you are referring any blog or tutorial go and download their code and see what version of dependencies they are using and install that dependencies in your project. Hope you solve your error, HAPPY CODING. :)
这是由于您在package.json中使用的依赖项版本,如果您引用任何博客或教程,请下载其代码并查看它们使用的依赖项版本,并在项目中安装该依赖项。希望你解决你的错误,快乐编码。 :)