2

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

Harshit
  • 1,510
  • 19
  • 42
Karthy Sbk
  • 3,153
  • 2
  • 8
  • 7

4 Answers4

1

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.

cpppatrick
  • 609
  • 3
  • 12
  • 29
  • 1
    I could see the response {message : 'user found')in postman. But not showing up in the website console – Karthy Sbk May 09 '20 at 18:45
  • So you're searching your db for a user, and if you found a user you're sending a message of "user found" to the front end. But in your code you're not actually sending the user data back. Try something like `return response.status(200).json({result})` – cpppatrick May 09 '20 at 19:11
  • I'm not getting response in data body on website console. I could get it in postman. In this case Result is true. Even this is not showing up. config: {url: "http://localhost:5000/users/login", method: "get", data: "{"email":"Karthik@gmail.com","password":"QWEERTTY"}", headers: {…}, transformRequest: Array(1), …} data: {} <----------empty array – Karthy Sbk May 09 '20 at 19:16
0

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.

Jérôme
  • 978
  • 1
  • 9
  • 22
0

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.

Panther
  • 8,938
  • 3
  • 23
  • 34
  • I tired. stiil not showing response message in console.config: {url: "http://localhost:5000/users/login", method: "get", data: "{"email":"Karthik@gmail.com","password":"QWEERTTY"}", headers: {…}, transformRequest: Array(1), …} data: {} headers: {content-length: "2", content-type: "application/json; charset=utf-8"} request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …} status: 200 statusText: "OK" – Karthy Sbk May 09 '20 at 18:50
0

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