post在setTimeout周围没有返回400"/>
axios.post在setTimeout周围没有返回400
app.post('/orders', (req, res) => {
console.log(req.body)
// more code
setTimeout(function () {
axios.post('', payload, { headers: headers })
.then(function (response) {
if (response.status === 201) {
res.sendStatus(201);
}
})
.catch(function (error) {
res.status(500).send({ error: error });
});
}, 500);
});
我在从test
端点获取400响应时遇到了麻烦,我发现当我用axios.post()
包裹setTimeout
时,最终给了我201响应。为什么是这样?我怎样才能更好地改善它?
根据我的经验,您无法直接从外部将变量传递到setTimeout函数中。您可以尝试创建全局变量并在setTimeout函数中访问它,也可以尝试使用getFunction来访问外部变量。
更多推荐
axios.post在setTimeout周围没有返回400
发布评论