Hapij回复被呼叫两次
我从下面的代码片段中得到了这个错误消息Hapij回复被呼叫两次,api,unit-testing,hapijs,Api,Unit Testing,Hapijs,我从下面的代码片段中得到了这个错误消息 Unhandled rejection Error: reply interface called twice 请注意,我对所有回复界面都使用return Locations .findOne({ _id: request.params.id, activationCode: payload.activationCode }).then((location) => { if (!location)
Unhandled rejection Error: reply interface called twice
请注意,我对所有回复界面都使用return
Locations
.findOne({
_id: request.params.id,
activationCode: payload.activationCode
}).then((location) => {
if (!location) {
return reply(Boom.notFound('Location not found'))
}
locationObject = location
if (payload.password !== payload.confirmPassword) {
return reply(Boom.badRequest('Password and Confirm Password must match'))
}
if (!payload.terms) {
return reply(Boom.badRequest('Must agree to Terms & Conditions'))
}
return newPaymentMethod.save()
}).then((paymentMethod) => {
.....
return user.save() // user is defined at .....
}).then(() => {
return reply({ token: helpers.createJwt(userObject) })
}).catch((err) => {
return reply(Boom.wrap(err))
})
任何帮助都将不胜感激。看来你是因为不正确地使用了承诺而陷入这种困境的。我猜您是在一个路由处理程序中执行代码片段,您可以在该处理程序中进行回复 当您在承诺链中返回响应时,您都会将值返回给下一个。然后承诺并从外部范围调用响应
我建议您使用promise reject for errors,这样您就只需要promise的.catch中的一个replyBoom.method。看起来您会因为不正确使用promises而陷入这种情况。我猜您是在一个路由处理程序中执行代码片段,您可以在该处理程序中进行回复 当您在承诺链中返回响应时,您都会将值返回给下一个。然后承诺并从外部范围调用响应
我建议您使用promise reject for errors,以便在promise的.catch中只需要一个replyBoom.method。,因为您最终链接了承诺
.then(() => {
return reply({ token: helpers.createJwt(userObject) })
}).catch((err) => {
return reply(Boom.wrap(err))
})
如果其中任何一个条件为真,您可以调用reply两次。
简单的解决方法是在条件为真时抛出错误-因为已经有一个Boom.wrap-in-catch块。,因为您最终得到了连锁承诺
.then(() => {
return reply({ token: helpers.createJwt(userObject) })
}).catch((err) => {
return reply(Boom.wrap(err))
})
如果其中任何一个条件为真,您可以调用reply两次。
简单的解决方法是在条件为真时抛出错误,因为在捕捉块中已经有Boom.wrap