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

后端 未结 8 1944
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:29

    You can manually generate the _id then you don't have to worry about pulling it back out later.

    var mongoose = require('mongoose');
    var myId = mongoose.Types.ObjectId();
    
    // then set it manually when you create your object
    
    _id: myId
    
    // then use the variable wherever
    

提交回复
热议问题