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