Reputation: 2933
I am working with node js & mongodb. Current I need some help to retrive _id for a saved document.
My Code :
var user= new User();
user.name = 'Name';
user.email = '[email protected]';
user.save(function(err){
if(!err)
{
console.log('Saved');
// Need this documents _id
}
})
How can I get this _id ? Need help for this.
Upvotes: 1
Views: 71
Reputation: 7077
you must add second arg for callback in save method.
var user= new User();
user.name = 'Name';
user.email = '[email protected]';
user.save(function(err, user){ // here you must add user
if(!err)
{
console.log('Saved');
user._id // id for user
}
})
Upvotes: 2