1

I am currently trying to query my backend using axios and to that specific address I am sending with res.json an object and I am also able to see it with postaman. But when trying to build a function to retrieve it, my object looks like:Promise {pending}. How can i refactor my function ?

   isAuthenticated = () => {
        return axios.get('https://myaddress/authenticate')
            .then(function (response) {
                return response.data
            })
    };

4
  • I suspect your code is lacking the catch block to handle the rejections. Also, I don’t see the Paramus are passed in.
    – CRayen
    Commented Nov 20, 2018 at 12:27
  • Have you tried implementing the catch callback on the promise ? You may have an error on the backend side. Commented Nov 20, 2018 at 12:27
  • Maybe you are using node under the HTTP and you are getting CORS because the axios is trying to get a HTTPS. Try to put the rejection function and/or change the URL to make sure that the axios code is working (I also can't see nothing wrong with it).
    – BrTkCa
    Commented Nov 20, 2018 at 12:30
  • Oj sorry. I just noticed that I get that response even for broken links..But then why I get the expected object with postman ?
    – some guy
    Commented Nov 20, 2018 at 12:35

2 Answers 2

1

You need to call the promise like so:

isAuthenticated().then(result => console.log(result))
.catch(error => console.log(error));
0

Use This code and let me know if still, you face a problem.

const isAuthenticated = () => { 
        return axios.get('https://myaddress/authenticate').then(response => {
            // returning the data here allows the caller to get it through another .then(...)
            return response.data
          }).catch(error => console.log(error));
     };

     isAuthenticated().then(data => {
        response.json({ message: 'Request received!', data })
      })

here is similar questions as yours: Returning data from Axios API || Please check it as well.

5
  • That second call, u pass data and return response.json ? or is it response.data.json ?
    – some guy
    Commented Nov 20, 2018 at 12:44
  • it is just response.json only. Please try it :) Commented Nov 20, 2018 at 12:47
  • It says response is undefined, because I think you are not passing it to function call
    – some guy
    Commented Nov 20, 2018 at 12:50
  • Can you please consol.log(data) in the second function? Commented Nov 20, 2018 at 12:56
  • I have put a catch in code so it catches error here so I can check it in a better way. And let know the outcomes Commented Nov 20, 2018 at 13:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.