Node.js Mongoose-更新后可以';我不能进入这个

Node.js Mongoose-更新后可以';我不能进入这个,node.js,mongoose,Node.js,Mongoose,当我试图访问它时,我正在使用mongoose和中间件。\u任何模型中的id都是未定义的 ClientSchema.post('update', function() { console.log(this._id); }); 你知道为什么吗 我的模式 var ClientSchema = new Schema({ name: { type: String, required: true }, company: {type: Schema

当我试图访问它时,我正在使用mongoose和中间件。\u任何模型中的id都是未定义的

ClientSchema.post('update', function() {
  console.log(this._id);
});
你知道为什么吗

我的模式

var ClientSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    company: {type: Schema.Types.ObjectId, ref: 'Company'}
});
ClientSchema.post('save', function() {
  console.log(this._id); ///working
});
ClientSchema.post('update', function() {
  console.log(this._id); ///undefined
});

结果应该作为参数传递给回调函数,如下所示

ClientSchema.post('update', function(res) {
  console.log(res._id);
});
您还可以使用承诺,这样您就可以捕获可能发生的错误

ClientSchema.post('update').then(function(res) {
   console.log(res._id)
}).catch(function(err) {
   console.error(err);
});
请看这里:

应:

ClientSchema.post('update', function(client) {
   console.log(client._id);
});
试试这个:

ClientSchema.post('update', function() {
   const document = this.getUpdate();
   console.log(document.id); ///working
});

仍然未定义@JenuRudan您可以看到错误,以便了解如何更好地调试什么是ClientSchema以及它是否有post方法?@Fortuneekeroo editedClient.update({u id:id},UpdatedAttribute,function(err){if(err){res.json({status:false,error:err});return;}res.json({状态:true,消息:'Client Updated'});});