In my nodejs project I'm fetching data from my MongoDB and retrieve after querying with this code:
app.get('/:user_id', (req, res) => {
Contact.findById(req.params.user_id, function (err, user) {
if (err){
res.send(err);
}
let user_temp = JSON.stringify(user)
console.log("1 - ", user);
console.log("2 - ", user.name);
console.log("3 - ", user_temp)
console.log("4 - ", user_temp.phone);
res.render('userInfo', {user: user})
});
})
The log I get is this
1 - {
_id: 5f56a47d5a246e5ff8b67129,
name: 'UserA',
phone: '054523423',
__v: 0
}
2 - undefined
3 - {"_id":"5f56a47d5a246e5ff8b67129","name":"UserA","phone":"054523423","__v":0}
4 - undefined
When I'm trying to console.log those values before or after JSON.stringify I always get undefined. I've looked at other bugs and I can't seem to find an explanation. What am I doing wrong?
let user_temp = JSON.stringify(user)before his line what doestypeof usergive? I guess it is the string that's why it is undefinedif (err) return res.send(err)else/foowill break with various errors