`
const data = [{'name': 'Apip'}, {'name': 'Rohmat'}, {'name': 'ujang'}]
data.forEach(function (response) {
res.json(response)
})
`
I did the code above it doesn’t work and then it throws an error "Cannot set headers after they are sent to the client"
2
Answers
You have to send the data in a batch:
See Why can’t I use res.json() twice in one post request?
If you want to just response the same array object, then you should write res.json(data).
But, if you are doing some check or modification within forEach to array object then it depend how you want the output.
You are getting this error is because, you have already sent result in the body, and forEach is trying to set the header again.