0

I am new to nodeJs but I already make research in order to solve my issue but it seems that I lacking a bit in async functionalities. I have found this solution but it does not work which I will post it down below : enter link description here

Here is my code

 router.put("/placeOrder", [auth, isVerified, isExists], async (req, res) => {
  try {
    const productList = req.body; // body
    await Promise.all(
      productList.map(async (element) => {
        // compare between the inserted element and the store one here
        let getItemPrice = await Order.findOne({
          _id: element._id,
          buyerId: req.user._id,
          orderStatus: "PENDING",
        });
        if (!getItemPrice) {
          return res.status(400).json({
            status: "failed",
            message: "order not exists",
          });
        }

        getItemPrice.items.map(async (item) => {
          await Order.findOneAndUpdate(
            {
              "items.productId": item.productId,
            },
            {
              $set: {
                orderStatus: "ACTIVE",
                "items.$.itemStatus": "ACTIVE",
                paymentMethod: element.paymentMethod,
              },
            }
          );
        });
      })
     
    );
    res.status(200).json({
      message: "Order has placed",
    });
  } catch (error) {
    return res.status(400).json({
      status: "failed",
      message: error.message,
    });
  }

  // update documents
});

I am trying to check if the Id does not exist within my array if not exists just through an error that Id does not exist, it is working fine but it keeps print error to the logs that say: TypeError: Cannot create property 'Symbol(level)' on string 'Cannot set headers after they are sent to the client' Thanks for your time and collerations

Abdulmalek
  • 105
  • 1
  • 10
  • 1
    It calls `res.status(400).json(` for **each** item that doesn't exist. – Quentin Jun 28 '21 at 18:53
  • You are probably calling res.json twice. Just add a console.log('send') before every res.json and if you see the log twice you need to make sure you only call it once. – Molda Jun 28 '21 at 18:54
  • Should I do a callback there and just print it once. Is that going to make it work – Abdulmalek Jun 28 '21 at 19:01

1 Answers1

0

You cannot send multiple responses.

productList.map(async (element, i) => {
    // compare between the inserted element and the store one here
    let getItemPrice = await Order.findOne({
      _id: element._id,
      buyerId: req.user._id,
      orderStatus: "PENDING",
    });

    getItemPrice && getItemPrice.items.map(async (item) => {
      await Order.findOneAndUpdate(
        {
          "items.productId": item.productId,
        },
        {
          $set: {
            orderStatus: "ACTIVE",
            "items.$.itemStatus": "ACTIVE",
            paymentMethod: element.paymentMethod,
          },
        }
      );
    });
  }
  • Yes, but in this case, I will not check if the _id: element._id, is exists or not. How I can check if the _id: element._id, is exist , in case if does not exist, I want to through an error – Abdulmalek Jun 29 '21 at 07:55