Axios可能解析響應。我訪問這樣的錯誤在我的代碼:
axios({
method: 'post',
responseType: 'json',
url: `${SERVER_URL}/token`,
data: {
idToken,
userEmail
}
})
.then(response => {
dispatch(something(response));
})
.catch(error => {
dispatch({ type: AUTH_FAILED });
dispatch({ type: ERROR, payload: error.data.error.message });
});
從文檔:
的請求的響應包含以下信息。
{
// `data` is the response that was provided by the server
data: {},
// `status` is the HTTP status code from the server response
status: 200,
// `statusText` is the HTTP status message from the server response
statusText: 'OK',
// `headers` the headers that the server responded with
headers: {},
// `config` is the config that was provided to `axios` for the request
config: {}
}
所以catch(error =>)
實際上只是catch(response =>)
編輯:
我仍然不明白爲什麼日誌記錄堆棧信息錯誤再次出現。我試圖像這樣記錄它。然後你可以看到它是一個對象。
console.log('errorType', typeof error);
console.log('error', Object.assign({}, error));
EDIT2:
後一些更多的環顧四周this是您要打印的內容。這是一個Javascipt錯誤對象。然後Axios用配置,代碼和響應來增強這個錯誤,如this。
console.log('error', error);
console.log('errorType', typeof error);
console.log('error', Object.assign({}, error));
console.log('getOwnPropertyNames', Object.getOwnPropertyNames(error));
console.log('stackProperty', Object.getOwnPropertyDescriptor(error, 'stack'));
console.log('messageProperty', Object.getOwnPropertyDescriptor(error, 'message'));
console.log('stackEnumerable', error.propertyIsEnumerable('stack'));
console.log('messageEnumerable', error.propertyIsEnumerable('message'));
感謝您的詳細回覆,我通過了幫助的存儲庫代碼。最終我登錄了對象並能夠看到響應對象並處理數據。 附加代碼: 'let e = {... error}' 'switch(e.response.data.type)' – Cnolimit