HapiJS 回复被调用两次

HapiJS reply being called twice

我收到以下代码段的错误消息

Unhandled rejection Error: reply interface called twice

请注意,我对所有 reply() 接口都使用 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))
    })

如有任何帮助,我们将不胜感激。

看来您是由于不正确地使用承诺而陷入困境的。我猜您是在可以访问 reply 的路由处理程序中执行您的代码片段。

当您 return 在承诺链中做出响应时,您既 return 下一个 .then (承诺)的价值,也调用 reply 来自外部范围。

我建议你使用 promise reject 来处理错误,这样你只需要在 promise 的 .catch().

中有一个 reply(Boom.method())

因为你最后链式承诺

.then(() => {
  return reply({ token: helpers.createJwt(userObject) })
}).catch((err) => {
  return reply(Boom.wrap(err))
})

如果任何 if 条件为真,您可能会调用 reply 两次。 简单的解决方案是在 if 条件为真时抛出错误 - 因为 catch 块中已经有一个 Boom.wrap