0

I am creating a e-commerce website, where when place an order api is call i am getting the error.

node:internal/process/promises:227 triggerUncaughtException(err, true /* fromPromise */); ^

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

    orderRouter.post(
  "/new",
  isAuth,
  expressAsyncHandler(async (req, res) => {
    if (req.body.orderItem.length === 0) {
      return res.status(404).send({ message: "Cart is empty" });
    } else {
      const order = new Order({
        orderItem: req.body.orderItem,
        shippingAddress: req.body.shippingAddress,
        paymentMethod: req.body.paymentMethod,
        user: req.body.user,
        statusId: req.body.statusId,
        isPaid: req.body.isPaid || null,
        // statusMsg = req.body.statusMsg || "No msg"
      });
      const createdOrder = await order.save();
      return res
        .status(200)
        .send({ message: "Order placed succefully", order: createdOrder });
    }
  })
);
  • 2
    Does this answer your question? [ERR\_HTTP\_HEADERS\_SENT: Cannot set headers after they are sent to the client](https://stackoverflow.com/questions/52122272/err-http-headers-sent-cannot-set-headers-after-they-are-sent-to-the-client) – Nir Alfasi Jun 02 '21 at 13:16
  • Also: https://stackoverflow.com/questions/60162480/error-err-http-headers-sent-cannot-set-headers-after-they-are-sent-to-the-clie and https://stackoverflow.com/questions/54301624/err-http-headers-sent-cannot-set-headers-after-they-are-sent-to-the-client – Nir Alfasi Jun 02 '21 at 13:16

0 Answers0