从客户端应用程序调用获取请求
axios.get('http://localhost:5000/users/login',user)
.then((res) => {
console.log(res);
});
获取请求API
User.findOne({email: email})
.then(docs => {
bcrypt.compare(password , docs.password, function(err, result) {
if(result){
return response.status(200).json({message : 'user found'})
}else{
return res.send(err);
} });})
.catch(Err => response.send(Err))
尝试一下:
User.findOne({email: req.body.email})
在您的API中,您必须指出电子邮件正文来自最前面的请求。您也可以尝试使用post
而不是get
,因为您是在请求中发送正文。
您正在尝试发送带有正文的GET请求,这很不常见。要发送带有axios
正文的GET请求,您需要遵循以下语法。
axios.get('http://localhost:5000/users/login',{
data: user
})
.then((res) => {
console.log(res);
});
还请确保您从正文中检索参数,而不是在GET Request中最常用的查询参数。
您也不会在响应中回送用户。
您有return response.status(200).json({message : 'user found'})
您正在获得答复(根据您帖子中的图片判断)。但是您也想发送result
。
请进行两项更改:
1)更改控制台日志数据获取
axios.get('http://localhost:5000/users/login',user)
.then((res) => {
console.log(res.data); //**<--- Here i change -->**
});
如果仍然发现任何错误,请进行第二次更改
2)更改您的请求数据
User.findOne({email: req.body.email}) //**<--Here i change-->**
.then(docs => {
bcrypt.compare(password , docs.password, function(err, result) {
if(result){
return response.status(200).json({message : 'user found'})
}else{
return res.send(err);
} });})
.catch(Err => response.send(Err))
通常,在API中,您必须通过正文请求数据,例如req.body.data