I can't seem to figure out why my code is not running correctly. While using Axios in my middleware, I am encountering this error message:
Error: Can't set headers after they are sent.
This is the snippet of my code (utilizing 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()
})
})
})
Even though I specify that I require calling
next()
, I still face the noted error.
I attempted to make the anonymous Lodash forEach function async
and apply an await
on the axios.post
, however, it didn't resolve the issue. I'm unsure about the exact location where the headers are being sent. What could be causing this problem?