Axios get request hangs, no error, catch not fired
Asked Answered
S

1

6

When I make a GET request with axios on node it just hangs, the catch is not thrown and error caught.

Am not sure how to debug this, no error is thrown. I am firing the spotify API, but surely if there was an issue on there side, I would get some response?

For a while I was getting a ECONNRESET error, my internet is not too stable. But this error stopped being throw.

I have tried using fetch, same issue. I have gone back to classic promise syntax. It has been working fine since now.

This method is called and logged.

"node": "10.0.0",
"axios": "^0.19.0",

    function tryFetchForPlaylists(usersCred) {
        console.log('req method called ', usersCred)
        let playlistData;

        try {
            playlistData = axios.get('https://api.spotify.com/v1/users/' + usersCred.userId + '/playlists',
                {
                    headers: {
                        'Authorization': 'Bearer ' + usersCred.accessToken,
                        'Content-Type': 'application/json'
                    }
                });

        } catch (err) {
            console.log(err)
            if (err.response.status === 401) {
                console.error(err);
                return {statusCode: 401};
            }
        }

        playlistData.then((res) => {
            console.info('response status',res.status)
            if(res.status === 200) {
                return res;
            }
        });
    }

'req method called 'is logged and the creds are there, nothing else, just hangs.

Scheming answered 19/7, 2019 at 15:4 Comment(3)
axios.get() returns a promise, not a finished value. You need to either use await or .then() on that promise to get the value. And, try/catch will only get errors if you're using await. Otherwise, you should use .catch() to see errors.Epa
@Epa It seems to be work if I use .then() and .catch() - i get the JSON in a 200 which is all good. But returning it to to a function that uses await does not work. ` let status = await tryFetchForPlaylists(userCreds); console.log('waiting for status ', status)` status is undefinedScheming
Well tryFetchForPlaylists() is just implemented wrong so you can't await it. You have to fix it's implementation. It seems like you need to do some reading about how you get errors from a promise. try/catch ONLY works for that inside an async function and with promises you are using await on. If you aren't doing await fetch(), then you have to use fetch().then().catch() to catch the errors.Epa
S
4

No need to store the call inside a function. Just use the call as a promise.

 function tryFetchForPlaylists(usersCred) {
        console.log('req method called ', usersCred)
        let playlistData;

        return axios.get('https://api.spotify.com/v1/users/' + usersCred.userId + '/playlists',
            {
                headers: {
                    'Authorization': 'Bearer ' + usersCred.accessToken,
                    'Content-Type': 'application/json'
                }
            })
            .then((data) => {
             // return your data here...
            })
            .catch((err) => {})


    }
Spitfire answered 19/7, 2019 at 15:52 Comment(3)
Yeah thanks, I had been using async and await in the try catch block before. Now seems to work with your configuration. I do get the JSON, but can't return to the caller function for some reason. I am using await in the caller function, but it is still undefined.Scheming
@Scheming I made some edits that should allow you to return the data to the caller. Make sure to return the axios promise then return the data as well. If this helps you solve your problem, would you mind marking it as the solution? Thanks!Spitfire
I forgot to return that axios function - damn! I will upvote you @SpitfireScheming

© 2022 - 2024 — McMap. All rights reserved.