Friday, 9 June 2017

axios.get returns promise object

So i've been trying to use axios in order to check whether or not a user is authenticated server side (Node+Express+Passport).

If a user is authenticated, the server sends back a "Connected" string, else, it sends back a "Not connected" string.

Router.get('/isAuth', function (req, res, next) {
    if (req.isAuthenticated()) {
        return res.status(200).send('Connected');
    } else {
        return res.status(200).send('Not connected');
    }
});

I wrote a simple function to check the server's response:

function isAuth() {
  return axios.get('http://localhost:3000/endpoints/isAuth')
  .then((response) => {
    if (response.data === "Connected") {
      return true;
    } else {
      return false;
    }
  })
  .catch((error) => {
    console.log(error)
  })
}

The function is supposed to return true if the user is connected, and vice-versa.

Now, when I try to log the the function's results in the console, this is what I get:

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
  __proto__ : Promise
  [[PromiseStatus]] : "resolved"
  [[PromiseValue]] : false

As you can see, the value returned is a promise object, whereas the desired output would preferably be a "True" or "False".



via Kyle Sentient

No comments:

Post a Comment