Best practice to handle errors in Mongoose's post hooks

时间:2015-07-08 15:43:43

标签: node.js mongodb express mongoose

For example, I define a post hook for the update event of a model schema. If a document is updated because of a request, then the post hook will trigger some async tasks. However, if any async task fails, I want to log this error with the request context so that I could know this error is caused by which request at least.

However, the post hook doesn't receive flow control, and I can't find an obvious way to pass the req object into the post hook too.

Reference: Mongoose.js Middleware

1 个答案:

答案 0 :(得分:0)

似乎Mongoose 4.0+开始支持异步帖子钩子(在https://github.com/Automattic/mongoose/pull/1977中找到),但documentation尚未相应更新...

使用异步帖子挂钩我应该能够使用next(err)处理错误,然后使用中间件来处理它(例如记录下来)。