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
            })
    };
some guy
  • 45
  • 1
  • 2
  • 8
  • I suspect your code is lacking the catch block to handle the rejections. Also, I don’t see the Paramus are passed in. – CRayen Nov 20 '18 at 12:27
  • Have you tried implementing the `catch` callback on the promise ? You may have an error on the backend side. – Multicolaure Nov 20 '18 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 Nov 20 '18 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 Nov 20 '18 at 12:35

2 Answers2

1

You need to call the promise like so:

isAuthenticated().then(result => console.log(result))
.catch(error => console.log(error));
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
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.