How do I get the objectID after I save an object in Mongoose?

后端 未结 8 2009
Happy的楠姐
Happy的楠姐 2020-12-05 03:37
var n = new Chat();
n.name = \"chat room\";
n.save(function(){
    //console.log(THE OBJECT ID that I just saved);
});

I want to console.log the ob

8条回答
  •  孤街浪徒
    2020-12-05 04:08

    Actually the ID should already be there when instantiating the object

    var n = new Chat();
    console.log(n._id) // => 4e7819d26f29f407b0... -> ID is already allocated
    

    Check this answer here: https://stackoverflow.com/a/7480248/318380

提交回复
热议问题