IN nodejs, with mongodb, mongoosejs as orm
I am doing this
I have a model, User
User.findOne({username:'someusername'}).exec(function(err,user){ console.log(user) //this gives full object with something like {_id:234234dfdfg,username:'someusername'} //but console.log(user._id) //give undefined. })
Why? And how to get the _id to string then?
Try this:
user._id.toString()
A MongoDB ObjectId is a 12-byte UUID can be used as a HEX string representation with 24 chars in length. You need to convert it to string to show it in console
using console.log
.
So, you have to do this:
console.log(user._id.toString());
Take the underscore out and try again:
console.log(user.id)
Also, the value returned from id is already a string, as you can see here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With