TypeError: Cannot read property '_id' of undefined
Asked Answered
F

5

17

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:

{
    "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.

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...
Faunie answered 30/8, 2013 at 22:25 Comment(1)
post the code where it is thrownImpractical
G
15

Make sure to npm install body-parser, then add

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

app.use(bodyParser());

to the top of your code. This is also assuming you are using Express.

Glottology answered 6/9, 2013 at 21:18 Comment(1)
Because you need to use body-parsing middleware to populate req.body. Otherwise you wont be able to parse the response.Glottology
H
7

Please try this:

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

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));
Headband answered 27/4, 2016 at 5:50 Comment(1)
The body-parser constructor was deprecated with Express 4. Source: #24330514Glottology
O
3

Add these two methods this worked for me:

app.use(express.json()); 
app.use(express.urlencoded({ extended: true }));
Obeah answered 22/2, 2022 at 5:10 Comment(1)
This is the best comment, cuz now in Express version 4.16+ they have included their own version of body-parser, so we don't need install body-parserRajah
T
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.

Touched answered 26/12, 2016 at 15:18 Comment(2)
What do you mean by this? Are you referring to express.json()?Schlessinger
package.json @hectorromerodevTouched
T
0

if you have an error as a result of express-jwt, replace the res.user._id with res.auth._id.

Translunar answered 30/10, 2022 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.