axios get 请求挂起,没有错误,catch 没有被触发
Axios get request hangs, no error, catch not fired
当我在节点上使用 axios 发出 GET 请求时它只是挂起,没有抛出 catch 并且捕获了错误。
我不确定如何调试它,没有抛出任何错误。我正在启动 spotify API,但如果这方面有问题,我肯定会得到一些回应吗?
有一段时间我收到 ECONNRESET 错误,我的互联网不太稳定。但是这个错误不再被抛出。
我试过使用 fetch,同样的问题。我又回到了经典的承诺语法。从现在开始它一直运行良好。
调用并记录此方法。
"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 方法'被记录下来,信用就在那里,没有别的,只是挂起。
无需将调用存储在函数中。只需将调用作为承诺。
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) => {})
}
当我在节点上使用 axios 发出 GET 请求时它只是挂起,没有抛出 catch 并且捕获了错误。
我不确定如何调试它,没有抛出任何错误。我正在启动 spotify API,但如果这方面有问题,我肯定会得到一些回应吗?
有一段时间我收到 ECONNRESET 错误,我的互联网不太稳定。但是这个错误不再被抛出。
我试过使用 fetch,同样的问题。我又回到了经典的承诺语法。从现在开始它一直运行良好。
调用并记录此方法。
"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 方法'被记录下来,信用就在那里,没有别的,只是挂起。
无需将调用存储在函数中。只需将调用作为承诺。
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) => {})
}