我无法理解为什么我的代码无法正确执行。在我的中间件中使用 Axios 时,我得到:
错误:发送后无法设置 header 。
这是我的代码(我正在使用 Lodash forEach):
app.post('/myApi', function (req, res, next) {
_.forEach(things, (thing) => {
axios.post(url, { data: thing })
.then(()=>{
// writing data to an excel file
})
.then(()=>{
// need this next to go to next middleware, but it causes the error mentioned above
next()
})
})
})
如你所见,我提到我需要打电话
next()
,但我收到了上述错误。
我尝试创建匿名 Lodash forEach 函数 async
并在 axox.post
上应用 await
,但这似乎并没有成功。工作。我不确定 header 到底发送到哪里。 我做错了什么?
最佳答案
由于您在 _forEach() 函数中调用了 next() ,这意味着您在每个循环上调用 next() ,这就是您提到的错误的原因。
关于javascript - Express 中间件中的 Axios 出现错误 - 发送 header 后无法发送 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53732399/