0
在我的註冊服務中,我創建了一個'before'鉤子來檢查用戶是否已經註冊了錦標賽。如果用戶被發現註冊,我會拋出一個錯誤。FeathersJS - 如何取消錯誤鉤子
每feathersjs文檔:
如果承諾失敗,錯誤會立即傳播,並會 將退出承諾鏈條。
我遇到的問題是錯誤被「觸發」並顯示用戶已經註冊,但註冊仍然發生。意思是,鉤子永遠不會「退出」。
這裏是我的功能
exports.checkRegistered =() => {
return (hook) => {
let tourneyId = hook.data.tourneyId
let userId = hook.data.userId
return hook.service.find({
query: {
tourneyId: tourneyId,
userId: userId
}
}).then(reg => {
// User already registered
if (reg.data.length != 0) {
// Also tried to return a promise with same result.
// return Promise.reject(new Error('Already registered.'))
throw new Error('Already registered');
}
}).catch(err => {})
}
}
這裏是我的 「前的鉤」 對象
exports.before = {
create: [
api.checkRegistered()
]
};
因爲那個投擲很可能會在當時被抓到。我的建議是回報一個承諾。 – Jalil
我試過返回Promise.reject(new Error('Error message')),但註冊仍然通過。我做錯了嗎? –
嗯..它應該工作。嘗試用承諾包裝它,就像示例[這裏](https://github.com/feathersjs/feathers-docs/blob/master/api/hooks.md#asynchronous-hooks) – Jalil