0

I need to get the value x-api-key that send me in the header in my end point with method get. the code in my end point is the next.in my localhost work fine. but in the server not work. the error is

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

router.get('/orders',async (req, res, next) => {
const apikey = req.header('x-api-key')
try {
    const customer = await Customer.find({apikey:apikey})
    const orders = await Order.find({ customer: ObjectId(customer[0]._id)})
    res.status(200).json(orders)
} catch (error) {
    res.status(400).json({"message":"Error"})
    next(error)
}

})

I've seen other posible solutions but I cant find the solution.

  • 1
    comment out the `res.status(400).json({"message":"Error"})` line, and handle the error in the next middleware / error handler, or remove the `next(error)` – Lawrence Cherone Mar 16 '21 at 23:34

2 Answers2

1

One cause of that problem is this:

res.status(400).json({"message":"Error"})
next(error)

You want to pick only one of those two options for sending the error response as they both try to send a response, thus you are trying to send two responses to the same request and you get the error you report.

When you call next(error) that will end up in your generic Express error handler (if you installed one or the default error handler if you did not) and will send an error response.

If you just want to send the error response here and be done with the request, then just remove the next(error). That should only be there if you want the general Express error handler to send the error response.


FYI, you will generally want to always log the actual error on your server so if this starts happening on your server, you can look at the logs and see what the lower level error is.

router.get('/orders',async (req, res, next) => {
    const apikey = req.header('x-api-key');
    try {
        const customer = await Customer.find({apikey:apikey});
        const orders = await Order.find({ customer: ObjectId(customer[0]._id)});
        res.status(200).json(orders)
    } catch (error) {
        console.log(error);
        res.status(400).json({"message":"Error"});
    }
})
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Error [ERR_HTTP_HEADERS_SENT]

According to another stackoverflow answer:

That particular error occurs whenever you try to send more than one response to the same request and is usually caused by improper asynchronous code.

Therefore, the point of error is most likely the next(error) part

router.get('/orders',async (req, res, next) => {
const apikey = req.header('x-api-key')
try {
    const customer = await Customer.find({apikey:apikey})
    const orders = await Order.find({ customer: ObjectId(customer[0]._id)})
    res.status(200).json(orders)
} catch (error) {
    res.status(400).json({"message":"Error"})
    try{next(error)} //this is most likely the source of your error
    catch(err){console.log(err)}
}
})
The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17