我正在尝试使用Promise链发送来自Promise的数据,但Promise已得到解决。但是catch
调用未运行then
调用
这是我使用Promise返回数据的代码:
UserSchema.statics.findByCredentials = function(email, password) {
const User = this;
return User.findOne({
email
}).then((user) => {
if(!user) {
return Promise.reject();
}
try {
return new Promise((reject, resolve) => {
bcrypt.compare(password, user.password, (err, res) => {
// console.log(res);
if(res)
resolve(user);
else
reject("Invalid Credentials");
});
});
}
catch(error) {
Promise.reject(error);
}
});
};
这是Promise链式通话,其中catch通话不起作用,则then通话
app.post('/users/login', (req, res) => {
const body = _.pick(req.body, ['email', 'password']);
// console.log(body);
User.findByCredentials(body.email, body.password)
.then((user) => {
}).catch(error => {
error.generateAuthToken().then((token) => {
res.header('x-auth',token).send(error);
});
});
});
您好,对代码进行了调整,以便再次调用。
有什么更好的解决方案。 确保已返回数据,这意味着Promise已解决。