I'm having an issue with a Promise that gets returned from MongoDB's mLab. Part of the data is passed correctly, rest is undefined. I'm not sure what is going on here.
This is JSON object from a collection in mLab that I connect to.
{
"_id": {
"$oid": "591424da932aad14186a7213"
},
"name": "Archos Elements 50 Oxygen",
"weight_oz": 4.59,
"battery_capacity_mAh": 2300,
"platform": "Android",
"date": {
"$date": "2017-05-11T08:46:18.684Z"
},
"dimensions": {
"width_mm": 70.5,
"height_mm": 143,
"depth_mm": 9.9
},
"__v": 0
}
This is how I retrieve the data (body-parser is used to capture 'name' of the phone that user inputs via HTML form)
router.post('/page_2', function(req, res){
phone = req.body.phone
mobile_and_tablet_model.find({name:phone}).exec(function(err, result) {
if (err)
throw err;
}).then(function(MongoDBdata){
console.log(MongoDBdata[0]) // logs out entire JSON object correctly
console.log(MongoDBdata[0]._id) // 5002
console.log(MongoDBdata[0].name) // Archos Elements 50 Oxygen
console.log(MongoDBdata[0].weight_oz) // undefined
console.log(MongoDBdata[0].battery_capacity_mAh) // undefined
console.log(MongoDBdata[0].platform) // Android
console.log(MongoDBdata[0].date) // 2017-05-11T08:06:27.894Z
console.log(MongoDBdata[0].dimensions) // undefined
res.render('page_2.hbs', {
title: 'some title',
phone: phone,
battery_capacity: MongoDBdata[0].battery_capacity_mAh, // undefined
platform: MongoDBdata[0].platform // Android
})
})
})
Can someone help me understand this behavior & help me fix it ?
I don't understand why am I getting 'undefined' for some nested values on the JSON object. I'm far from being an expert on Promises, but if I understand correctly then promise has been resolved, so the sync/async behavior should not play a role here ? (especially since console.log(MongoDBdata[0]) logged out the JSON content in full).
Much appreciated!
via user7861743
No comments:
Post a Comment