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

后端 未结 8 1996
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:10

    Well, I have this:

    TryThisSchema.post("save", function(next) {
        console.log(this._id);
    });
    

    Notice the "post" in the first line. With my version of Mongoose, I have no trouble getting the _id value after the data is saved.

提交回复
热议问题