1
我正在使用Q promise庫進行鏈接查詢。我有帖子掛鉤添加到新文檔的新屬性。Mongoosejs在創建後發佈保存
MyModel.create(data)
.then(function(data) {
// <------------------ post hook has not been triggered yet
return MySchema.findById(data._id); // trying to get the new document
})
.then(function(data) {
// <------------------ post hook has not been triggered yet
reply(null, data); // <- my callback
});
.done()
// <------------------ post hook has triggered!!!
在創建新文檔後掛鉤不觸發,並在節點控制檯中,我看到它在該鏈結束後觸發。
我可以如何確保在返回新文檔之前創建post hook觸發器?
更新:我的郵件鉤子是異步的,並且貓鼬不會等待它。沒有EventEmitter,沒有其他方式可以使用post hook。
當posthook完成時已經制定了發射自定義事件的解決方案。這裏http://pastebin.com/bGh3mPxJ mongoose執行所有鉤子,不要等到它們完成。因此無法知道文檔何時準備就緒。 – Dmytro