【发布时间】:2021-05-04 15:29:05
【问题描述】:
假设我想在 Express 邮寄路线内对数据库进行 Mongoose 查询:
app.post("/login",(req,res)=>{
const username = req.body.username
const password = req.body.password
User.find({username:username},(err,user)=>{
if (err) handleError(err)
//if user exists
if (user.length) {
//check password
if (user.password === password) {
//assign jwt, redirect
} else {
//"username/password is incorrect"
}
} else {
//"username/password is incorrect"
}
})
})
我关心的是 handleError 函数。我不太确定在 Mongoose 中甚至会发生什么样的错误,因为它只是一个简单的查询,但是应该在 handleError 函数中包含什么?那时我应该向用户发送什么响应?
【问题讨论】:
标签: javascript mongodb rest express mongoose