未处理的PromiseRejectionWarning:TypeError:res.status(..).json(..



当我试图使用poster发布请求时,收到一个错误Type Error: res.status(...).json(...).catch is not a function,不知道我做错了什么。

signin.js

exports.signin = (req, res) => {
const { email, password } = req.body;
if (!email || !password) {
res.status(422).json({
error: "please enter email and password"
})
}
User.findOne({ email: email })
.then(SavedUser => {
if (!SavedUser) {
return res.status(400).json({
error: "invalid email or password"
})
}
bcrypt.compare(password, SavedUser.password)
.then(doMatch => {
if (doMatch) {
res.json({
message: "Successfully Signed in"
})
}
else {
return res.status(422).json({
error: "Invalid email or password"
})
.catch(err => {
console.log(err);
})
}
})
})
}

您放错了.catch(...),它应该在.then(...)之后,而不是res.json():


exports.signin = (req, res) => {
const { email, password } = req.body
if (!email || !password) {
res.status(422).json({
error: 'please enter email and password'
})
}
User.findOne({ email: email })
.then(SavedUser => {
if (!SavedUser) {
return res.status(400).json({
error: 'invalid email or password'
})
}
bcrypt.compare(password, SavedUser.password)
.then(doMatch => {
if (doMatch) {
res.json({
message: 'Successfully Signed in'
})
} else {
return res.status(422).json({
error: 'Invalid email or password'
})
}
})
.catch(err => { // .catch goes here
console.log(err)
})
})
}

相关内容

最新更新