【发布时间】:2021-04-17 05:23:14
【问题描述】:
我使用 api,当 api 返回 200 状态码时,我返回响应,但是当 api 返回 400 状态码时,api 返回带有错误的数组,我的问题是如何获取此数组错误并返回此数组.
代码
try {
const config = {
method: 'get',
url: 'http://localhost:4000/api/orders',
headers: {'Key': '96db259b-2239-4abb-9b9d-a682a1de6b3c'}
}
const result = await axios(config)
return result.data
} catch (error) {
console.log('error ' + error)
returns result.data.errors
}
这个状态码的响应是 400。
"errors": [
{
"value": "96db259b-2239-4abb-9b9d-a682ssa1de6b3c",
"msg": "the API key 96db259b-2239-4abb-9b9d-a682ssa1de6b3c is invalid",
"param": "key",
"location": "headers"
}
]
【问题讨论】:
标签: javascript node.js express axios