【发布时间】:2020-11-28 18:29:21
【问题描述】:
我正在尝试为我的模型(用户)添加登录功能。我检查了我的数据库连接,它运行良好,因为其他路由运行良好。就像我创建用户时一样,它会成功创建用户。所以我确信我的数据库或我的数据库连接没有问题。甚至少数我的代码都可以正常工作。
我正在发送带有以下信息的 POST 请求
{
"email":"sherryijan@gmail.com",
"password":"6421515"
}
app.post("api/users/login", (req, res)=>{
//find email
User.findOne({email: req.body.email}, (err, user) =>{
if(!user) return res.json({
loginSuccess: false,
message: "Auth Failed, email not found"
});
//compare password
user.comparePassword(req.body.password, (err, isMatch) => {
if(!isMatch){
return res.json({loginSuccess:false, message:"wrong password"})
}
})
//generate token
user.generateToken((err, user)=>{
if(err) return res.status(400).send(err);
res.cookie("x_auth", user.token)
.status(200)
.json({
loginSuccess: true
})
})
})
})
它显示的错误是
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/users/login</pre>
</body>
</html>
我正在使用邮递员。
【问题讨论】:
标签: node.js mongodb express mongoose postman