最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 3浏览 0评论

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

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

我的代码:

login: (parent, { email, password }, context, info) => {
          User.findOne({ email }, function(err, user) {
            if (user.length) {
              bcryptpare(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");
            }
          });
    }
回答如下:

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

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

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

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

我的代码:

login: (parent, { email, password }, context, info) => {
          User.findOne({ email }, function(err, user) {
            if (user.length) {
              bcryptpare(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");
            }
          });
    }
回答如下:

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

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);
};
发布评论

评论列表 (0)

  1. 暂无评论