Sails JS helper错误解决/拒绝

时间:2018-11-08 14:18:44

标签: node.js sails.js

我正在使用Sails中的Helpers进行登录。 这是我的 helpers / login.js

  fn: async function (inputs, exits) {
    const password = inputs.password;
    const email = inputs.email;

    // find user with email
    const user = await User.findOne({email});
    if (user){
      const salt = user.salt;
      const hashedPass = user.password;
      const iterations = user.iterations;

      // check if input password matches with password in DB
      crypto.pbkdf2(password, salt, iterations, 64, 'sha512',
        (err, key) => {
          if (!err) {
            if (hashedPass === key.toString('hex')) {
              // password matched
              return exits.success({code: 200});
            }

          }
        });
    }
    // account not found or password doesnt match
    return exits.success({code: 404});


  }

UserController.js

login: async function(req, res){
    let loginUser;
    try {    
      loginUser = await sails.helpers.login.with({
        email: req.body.email, password: req.body.password
      });


    } catch (e) {
      console.log("Error login in usercontroller ", e.message);
      return res.serverError();
    }

    if (loginUser.code == 200) {
      return res.ok();

    }else {
      return res.serverError();
    }

  }

问题出在我有正确 电子邮件密码的帮助器中,尽管它返回了code: 200返回code: 404。收到来自节点的错误消息:

WARNING: Something seems to be wrong with this function.
It is trying to signal that it has finished AGAIN, after
already resolving/rejecting once.
(silently ignoring this...)

,当我输入错误的用户名/电子邮件时,同样会返回该消息。但是,当我删除return exits.success({code: 404})并输入正确的电子邮件和密码后,它将返回正确的代码(200)。我需要修复它的帮助。

1 个答案:

答案 0 :(得分:1)

问题可能是您试图返回错误代码作为“成功”出口。在exits对象中,应制作一个类似notFound: { responseType: 'notFound' }的对象,然后在要在代码中使用它时,执行return exits.notFound()。这是一个示例:https://sailsjs.com/documentation/concepts/actions-and-controllers#?actions-2