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.
axios.get()
returns a promise, not a finished value. You need to either useawait
or.then()
on that promise to get the value. And,try/catch
will only get errors if you're usingawait
. Otherwise, you should use.catch()
to see errors. – Epa.then()
and.catch()
- i get the JSON in a 200 which is all good. But returning it to to a function that usesawait
does not work. ` let status = await tryFetchForPlaylists(userCreds); console.log('waiting for status ', status)` status isundefined
– SchemingtryFetchForPlaylists()
is just implemented wrong so you can'tawait
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 usingawait
on. If you aren't doingawait fetch()
, then you have to usefetch().then().catch()
to catch the errors. – Epa