简体   繁体   English

MongoDB / mongoose-发布保存挂钩未运行

[英]MongoDB/mongoose - Post save hook not running

I have this model/schema: 我有这个模型/模式:

const InviteSchema = new Schema({
  inviter: {type: mongoose.Schema.Types.ObjectId, ref: 'Account', required: true},
  organisation: {type: mongoose.Schema.Types.ObjectId, ref: 'Organisation', required: true},
  sentTo: {type: mongoose.Schema.Types.ObjectId, ref: 'Account', required: true},
  createdAt: {type: Date, default: new Date(), required: true}
});

InviteSchema.post('save', function(err, doc, next) {

  // This callback doesn't run
});

const Invite = mongoose.model('Invite', InviteSchema);

module.exports = Invite;

Helper function: 辅助功能:

exports.sendInvites = (accountIds, invite, callback) => {

  let resolvedRequests = 0;

  accountIds.forEach((id, i, arr) => {

    invite.sentTo = id;

    const newInvite = new Invite(invite);

    newInvite.save((err, res) => {

      resolvedRequests++;

      if (err) {

        callback(err);

        return;
      }

      if (resolvedRequests === arr.length) {
        callback(err);
      }
    });
  });
};

And the router endpoint which calls the helper function: 以及调用帮助程序功能的路由器端点:

router.put('/organisations/:id', auth.verifyToken, (req, res, next) => {

  const organisation = Object.assign({}, req.body, {
    updatedBy: req.decoded._doc._id,
    updatedAt: new Date()
  });

  Organisation.findOneAndUpdate({_id: req.params.id}, organisation, {new: true}, (err, organisation) => {

    if (err) {
      return next(err);
    }

    invites.sendInvites(req.body.invites, {
      inviter: req.decoded._doc._id,
      organisation: organisation._id
    }, (err) => {

      if (err) {
        return next(err);
      }

      res.json({
        error: null,
        data: organisation
      });
    });
  });
});

The problem here is that the .post('save') hook doesn't run, despite following the instructions, ie using .save() on the model instead of .findOneAndUpdate for example. 这里的问题是,尽管遵循了说明,但.post('save')钩子仍未运行,即在模型上使用.save()而不是例如.findOneAndUpdate I've been digging for a while now but I cannot see what the problem here could be. 我已经挖掘了一段时间,但我看不出这里可能是什么问题。

The Invite document(s) are saved to the database just fine so the hook should fire, but doesn't. Invite文档可以很好地保存到数据库中,因此该钩子应该触发,但不会。 Any ideas what could be wrong? 任何想法可能有什么问题吗?

You can declare the post hook with different number of parameters. 您可以使用不同数量的参数声明发布挂钩。 With 3 parameters you are treating errors, so your post hook will be called only when an error is raised. 使用3个参数可以处理错误,因此仅在引发错误时才调用post挂钩。 But, if your hook has only 1 or 2 parameters, it is going to be executed on success. 但是,如果您的钩子只有1或2个参数,它将在成功执行。 First parameter will be the document saved in the collection, and second one, if passed, is the next element. 第一个参数是保存在集合中的文档,第二个参数(如果传递)是下一个元素。 For more information, check official doc: http://mongoosejs.com/docs/middleware.html Hope it helps. 有关更多信息,请查看官方文档: http : //mongoosejs.com/docs/middleware.html希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM