obiwankenoobi
obiwankenoobi

Reputation: 1582

empty response return from server

I am trying to fetch some data from my server and when testing the request with Postman it works great and I getting data back. But then when I trying to implement the request in my app Im getting back an empty object with undefined properties

CODE:

fetchUserData = (user) => {
  axios.post('api', {
    username: user
  })
  .then((res) => {
    this.setState({
      credit: res.credit,
      points: res.points
    })
    console.log(`this is user stats\ncredit:${res.credit}\npoints:${res.points} for the user: ${user}`)
  })
}


router.post('/', (req, res) => {

    UserData.findOne({                    
        username: req.body.username,                         
    }, 

    (err, user) => {
        if (err) {
            console.log('Error in saving user to mongodb: ' + err);
            res.send(err);
        }
        if (user) {
            if(user.username === req.body.username) {
                console.log('fetching data for: ' + user.username);
                console.log(user);
                res.send(user);
            }
        }
    });

})

RESPONSE FROM POSTMAN:

{"_id":"5afd9bXXXXXXX664a5af","username":"check","credit":1,"points":0,"__v":0}

RESPONSE ON APP CONSOLE:

this is user stats credit:undefined points:undefined for the user: check

Upvotes: 1

Views: 219

Answers (1)

obiwankenoobi
obiwankenoobi

Reputation: 1582

to read data from the response I should have to use 'res.data.something' and not res.something

Upvotes: 2

Related Questions