Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Axios在点击API后未显示数据响应_Reactjs_Mongodb_Axios_Bcrypt_Jsonresponse - Fatal编程技术网

Reactjs Axios在点击API后未显示数据响应

Reactjs Axios在点击API后未显示数据响应,reactjs,mongodb,axios,bcrypt,jsonresponse,Reactjs,Mongodb,Axios,Bcrypt,Jsonresponse,从客户端应用调用GET请求 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, resul

从客户端应用调用GET请求

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请求中常用的查询参数。

您也没有在响应中返回用户

您有
返回response.status(200).json({message:'user found'})

你得到了回复(从你帖子中的图片判断)。但是您还想发送
结果

请执行两项更改:

1) 更改控制台日志数据获取

axios.get('http://localhost:5000/users/login',user)
 .then((res) => {
    console.log(res.data); //**<--- Here i change -->**             
 });
axios.get('http://localhost:5000/users/login,用户)
。然后((res)=>{
console.log(res.data);//***
});
如果您仍然发现任何错误,请进行第二次更改

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))
User.findOne({email:req.body.email})//****
。然后(docs=>{
bcrypt.compare(密码、docs.password、函数(错误、结果){
如果(结果){
返回response.status(200).json({message:'user found'})
}否则{
返回res.send(err);
} });})
.catch(Err=>response.send(Err))

一般来说,在API中,您必须通过body请求数据,例如,req.body.data

我可以在postman中看到响应{message:'user found')。但没有显示在网站控制台中,因为您正在数据库中搜索用户,如果您找到了用户,则发送“user found”消息但是在你的代码中,你实际上并没有把用户数据发回。试试类似于
returnresponse.status(200).json({result})的方法
我在网站控制台上的数据体中没有收到响应。我可以在postman中得到响应。在这种情况下,结果是真的。即使没有显示。配置:{url:,方法:“get”,数据:{email:”Karthik@gmail.com,“密码”:“QWEERTTY”},标题:{…},transformRequest:Array(1),…}数据:{}仅供参考,我在const-email中存储了req.body.email。然后选择第一个选项并让我知道。我累了。仍然没有在console.config:{url:,方法:“get”,数据:{“email”:Karthik@gmail.com“,”密码“:”QWEERTTY“}”,头:{…},转换请求:数组(1),…}数据:{}”标题:{content length:“2”,内容类型:“application/json;charset=utf-8”}请求:XMLHttpRequest{readyState:4,超时:0,带凭据:false,上载:XMLHttpRequestUpload,onreadystatechange:ƒ,…}状态:200状态文本:“OK”