hapi.js: Error: reply interface called twice?

发布于 2022-09-07 08:29:26 字数 2538 浏览 21 评论 0

这个错误是不是因为我的代码中多次调用了reply这个接口?我看文档官方好像对这个接口做了限制,怎么可以解决多次调用产生的这个错误呢?

(node:5795) UnhandledPromiseRejectionWarning: Error: reply interface called twice
    at Object.exports.assert (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hoek/lib/index.js:736:11)
    at Function.internals.response (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hapi/lib/reply.js:164:10)
    at bound (domain.js:301:14)
    at Function.runBound (domain.js:314:12)
    at reply (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hapi/lib/reply.js:72:22)
    at bound (domain.js:301:14)
    at runBound (domain.js:314:12)
    at result.then (/Users/labikemmy/Downloads/React-Native-FriendChat/api/node_modules/hapi/lib/handler.js:105:36)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)
(node:5795) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5795) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
null

示例代码:

export default async function (request, reply) {
  if (request.auth.credentials.email !== request.payload.email) {
    await User.findOne({ email: request.auth.credentials.email }).then(
      (user) => {
        if (user) {
          User.findOne({ email: request.payload.email }).then(
            (friend) => {
              if (friend) {
                const stringId = `${friend._id}`;
                const friendExists = user.friends.filter(f => `${f}` === stringId).length > 0;
                if (!friendExists) {
                  user.friends.push(friend);
                  user.save();
                  reply({ friend: { fullName: friend.fullName, _id: friend._id } });
                } else {
                  reply(Boom.conflict('You have added already this friend'));
                }
              } else {
                reply(Boom.notFound(`Friend ${request.payload.email} doesn't exist`));
              }
            },
          );
        } else {
          reply(Boom.notFound('Cannot find user'));
        }
      },
    );
  } else {
    reply(Boom.conflict('Cannot add yourself as a friend'));
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文