未处理的拒绝TypeError:`next()`当我给护照提供错误的凭据时多次被调用

问题描述 投票:0回答:1
  new LocalStrategy({ session: false }, (username, password, done) => {
    User.findOne({ where: { email: username } })
      .then(user => {
        if (!user) {
          logger.error(`user not found: `, { username, password });
          return done(null, false, { message: 'user not found.' });
        }
        if (!user.password) {
          return done(null, false, { message: 'Please reset your password!' });
        }
        return Promise.all([user, user.isValidPassword(password)]);
      })
      .then(([user, isValid]) => {
        if (!isValid) {
          return done(null, false, { message: 'user not found.' });
        }
        return done(null, user);
      })
      .catch(done);
  })
);

在上面,当我发送不正确的凭据时,您会看到我的护照本地策略,它会给我下面的错误提示,如果我给错用户名并输入正确的用户名,为什么会出现错误?发生

Unhandled rejection TypeError: `next()` called multiple times
    at dispatch (/Users/imanthaatapattu/apps/bdata-api/node_modules/compose-middleware/lib/index.js:30:23)
    at next (/Users/imanthaatapattu/apps/bdata-api/node_modules/compose-middleware/lib/index.js:37:24)
    at Strategy.strategy.error (/Users/imanthaatapattu/apps/bdata-api/node_modules/passport/lib/middleware/authenticate.js:343:9)
    at verified (/Users/imanthaatapattu/apps/bdata-api/node_modules/passport-local/lib/strategy.js:81:28)
    at bound (domain.js:419:14)
    at runBound (domain.js:432:12)
    at tryCatcher (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/promise.js:604:18)
    at Promise._settlePromise0 (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/promise.js:649:10)
    at Promise._settlePromises (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/promise.js:725:18)
    at _drainQueueStep (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/async.js:93:12)
    at _drainQueue (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (/Users/imanthaatapattu/apps/bdata-api/node_modules/bluebird/js/release/async.js:15:14)
    at processImmediate (internal/timers.js:439:21)
    at process.topLevelDomainCallback (domain.js:130:23)
node.js passport.js
1个回答
0
投票

我认为这是因为您嵌套了then块。

在失败的情况下,在done()块中返回then的结果(即使未定义)仍将移至下一个then块,其中将再次调用done

在成功的情况下,您不会看到错误,因为没有满足任何失败条件,因此done仅被调用一次。

改为尝试抛出错误,然后仅在done块中处理一次catch的失败案例:

new LocalStrategy({ session: false }, (username, password, done) => {
  User.findOne({ where: { email: username } })
    .then(user => {
      if (!user) {
        logger.error(`user not found: `, { username, password });
        throw new Error('user not found.');
      }
      if (!user.password) {
        throw new Error('Please reset your password!');
      }
      return Promise.all([user, user.isValidPassword(password)]);
    })
    .then(([user, isValid]) => {
      if (!isValid) {
        throw new Error('user not found.');
      }

      // Success
      return done(null, user);
    })
    .catch(err => {
      // Failure
      done(null, false, { message: err.message });
    })
});
© www.soinside.com 2019 - 2024. All rights reserved.