Axios not showing data response after hitting the API

Viewed 2633

Calling GET request from the client app

axios.get('http://localhost:5000/users/login',user)
 .then((res) => {
    console.log(res);             
 });

GET request 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))

postman response message

empty body array from client console

4 Answers

You're also not sending the user back in your response.

You have return response.status(200).json({message : 'user found'})

You're getting the response back (judging by the picture in your post). But you also want to send the result.

Try this :

User.findOne({email: req.body.email})

In your API, you must indicate that the email body is from the front request. You can also try with post instead of get because you're sending a body into your request.

You are trying to send a GET request with a body which is quite uncommon. To send a GET request with body in axios you need to follow the below syntax.

axios.get('http://localhost:5000/users/login',{
  data: user
})
 .then((res) => {
  console.log(res);             
});

Also make sure you are retrieving the params from the body instead of the query params which are mostly used in GET Request.

Please do two Changes:

1)Change console log data fetching

axios.get('http://localhost:5000/users/login',user)
 .then((res) => {
    console.log(res.data); //**<--- Here i change -->**             
 });

if you find still any error then go for 2nd change

2) change your request data

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))

generally, in API, you have to request your data through body eg. req.body.data

Related