Hapi.js UnhandledPromiseRejectionWarning:错误:回复接口被调用两次?

时间:2018-05-21 05:48:22

标签: javascript hapijs

当我运行我的项目时,我收到错误:

(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

我不知道这是错误还是我的代码错误?我屏幕hapi.js问题,有人说错误是错误,另一个说回复()次数是相同请求的限制&#39;?如果它是有限的,如何更改下面的代码?

```

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'));
  }
}

Hapi@16.4.1

1 个答案:

答案 0 :(得分:0)

你有任何其他插件或生命周期钩子,如onPreHandler或其他什么?也许有一点你的代码抛出了这个错误,因为你(或你的代码以某种方式)在你的实际响应之前调用了回复接口。

另外,我重构了你的代码。您已经在使用JavaScript异步接口,因此您不需要使用&#34;然后&#34;呼唤你的承诺。

试试这个,看看会发生什么:

export default async function (request, reply) {

    if (request.auth.credentials.email === request.payload.email) {
        return reply(Boom.conflict('Cannot add yourself as a friend'))
    }

    // I belive this is mongoose model
    const user = await User.findOne({email: request.auth.credentials.email}).exec();
    if (!user) {
        return reply(Boom.notFound('Cannot find user'));
    }

    const friend = await User.findOne({email: request.payload.email}).exec();

    if (!friend) {
        return reply(Boom.notFound(`Friend ${request.payload.email} doesn't exist`));
    }

    const stringId = `${friend._id}`;
    const friendExists = user.friends.filter(f => `${f}` === stringId).length > 0;
    if (!friendExists) {
        // hmmm shouldn't it be friend._id? user.friends.push(friend._id.toString());
        user.friends.push(friend);
        // better use this statement
        // ref: http://mongoosejs.com/docs/api.html#document_Document-markModified
        user.markModified('friends');
        await user.save();
        return reply({friend: {fullName: friend.fullName, _id: friend._id}});
    } else {
        return reply(Boom.conflict('You have added already this friend'));
    }
}