这是我的阿波罗解析器和我想回报用户,但它不返回任何东西,如果我返回查询本身的话,我不能做密码检查
我的代码:
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");
}
});
}
我做了这个解决方案,它可以正常使用
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);
};