【发布时间】:2020-09-19 06:46:46
【问题描述】:
我正在尝试比较密码以登录用户,但我不断收到“此请求没有响应。”
这是我的发帖路线。
router.route("/login/:username1").post(async (req, res) => {
const userexist = User.find(username=>userexist.username=req.body.username1)
if(userexist == null)
{
return res.status(400).json("Cannot find user");
}
try
{
if(await bcrypt.compare(req.body.password, userexist.password))
{
res.json("success");
}
else
{
res.json("Not Allowed");
}
} catch {
res.status(500).json();
}
});
这是我在登录页面的发布方法。
onSubmit(e) {
e.preventDefault();
const isValid = this.validate();
const username1=this.state.username;
if (isValid) {
const user = {
username: this.state.username,
password: this.state.password,
};
axios
.post("http://localhost:5000/users/login/"+ username1, user)
.then((res) => console.log(res.data));
this.setState(initialState);
}
}
【问题讨论】:
-
username=>userexist.username?也许你的意思是user => user.username? -
我不这么认为,如果我这样做,我会尝试按用户名搜索,然后我不会得到在正文中传递的用户名。那将是一个空用户名仪式?我想也许它也与搜索参数有关。
-
userexist在过滤器函数中未定义。所以正确的代码是const userexist = User.find (user => user.username == req.body.username1)。附:是的,还有一个错误:使用==而不是=进行比较。 -
谢谢,但是当我发布请求时,它会中断 mongo db 连接。并且它不接受 == 来比较它需要 === 奇怪..用户不应该是用户名,因为它在我的架构中?
标签: node.js reactjs mongodb react-router bcrypt