【发布时间】:2018-03-26 14:55:27
【问题描述】:
我正在尝试使用简单的 pre-hook Mongoose 来删除这样的文档的任何引用:
PostSchema.pre("remove", async () => {
await Comment.remove({ _postId: this._id }).exec();
await User.update({ $pull: { _posts: this._id } }).exec();
});
上面的粗箭头语法似乎不起作用 - 尽管 Post 文档被删除,但 Comment 和 User 模型并没有相应地更新。相反,我不得不使用旧语法(根据 mongoose 文档)来让钩子正常工作,例如:
PostSchema.pre("remove", async function() {
await Comment.remove({ _postId: this._id }).exec();
await User.update({ $pull: { _posts: this._id } }).exec();
});
我觉得这很奇怪,除非我做错了什么。这是预期的行为吗?
【问题讨论】: