我没有得到的猫鼬查询用户的返回值

问题描述 投票:0回答:1

这是我的阿波罗解析器和我想回报用户,但它不返回任何东西,如果我返回查询本身的话,我不能做密码检查

我的代码:

login: (parent, { email, password }, context, info) => {
          User.findOne({ email }, function(err, user) {
            if (user.length) {
              bcrypt.compare(password, user[0].password, function(err, res) {
                if (res) {
                  console.log(user[0]); // this has the user
                  return user[0]; // but this does not return anything
                } else {
                  throw new ApolloError("failed");
                }
              });
            } else {
              throw new ApolloError("failed");
            }
          });
    }
javascript node.js mongoose react-apollo apollo-server
1个回答
0
投票

我做了这个解决方案,它可以正常使用

login: async (parent, { email, password }, context, info) => {
      const user = await attemptSignIn(email, password);

      return user;
    }

const attemptSignIn = async (email, password) => {
  const message = "Incorrect email or password. Please try again.";

  const user = await User.findOne({ email });

  if (!user || !(await matchesPassword(user, password))) {
    throw new AuthenticationError(message);
  }

  return user;
};

matchesPassword = (user, password) => {
  return compare(password, user.password);
};
© www.soinside.com 2019 - 2024. All rights reserved.