I have a simple POST
route:
// -------------- CREATE Event --------------
app.post('/events', (req, res) => {
var event =
{
title : req.body.title,
description : req.body.description,
address : {
street: req.body.address.street,
city : req.body.address.city,
state : req.body.address.state
}
}
db.collection('events').insert( event, (err, result) => {
if (err) {
res.send({ 'error': 'An error has occurred' })
}
else {
// RETURNS: "{street : \"streetname\", city : \"cityname\", state : \"statename\"}"
console.log(JSON.stringify( req.body.address ))
// RETURNS: {"title":"First Event","description":"this is my first event title","address":{},"_id":"591f512b15d612fc110dd31f"}
console.log(JSON.stringify(result.ops[0]))
res.send(result.ops[0])
}
})
// RETURNS: {street : "streetname", city : "cityname", state : "statename"}
console.log(req.body.address)
})
When I submit a POST request via Postman, the embedded object's value results in null when created, but the req.body.address
outside of the .insert()
method shows that the ` is intact.
{
"_id" : ObjectId("591f49519eb9e5facb6352fe"),
"title" : "First Event",
"description" : "this is my first event title",
"address" : {
"street" : null,
"city" : null,
"state" : null
}
}
tl;dr How am I able to send an embedded object to MongoDB without the embedded object's values turning to null?
NOTES:
- Yes I am including
app.use(bodyParser.json())
andapp.use(bodyParser.urlencoded({ extended: true }))
- Using latest versions of body-parser, express and mongodb
- Using
x-www-form-urlencoded
on data-type for Postman
via Steven_
No comments:
Post a Comment