node mongoose parse JSON payload in request.body node mongoose parse JSON payload in request.body mongoose mongoose

node mongoose parse JSON payload in request.body


To instantiate a new Mongoose.model object using a JSON payload in request.body, simply set each field as seen below.

Confusion arrises when anything similar to the following is attempted:

var x = new X(request.body). 

That being said, I would like to know if the above code is possible.

The request header must have Content-Type: application/json, and the following body-parser code should be present. In my case, address contains several json fields, so it appears that body parser should work in all scenarios if you do what I did below.

var bodyParser = require('body-parser');app.use(bodyParser.urlencoded({ extended: false }))app.use(bodyParser.json()) var user = new users({            _id: request.body._id,            email:request.body.email,            account:request.body.account,            name:request.body.name,            address:request.body.address,            phone:request.body.phone        });