将标头发送到客户端后无法设置标头'

时间:2021-06-28 18:45:42

标签: javascript node.js arrays mongodb mongoose

我是 nodeJs 的新手,但我已经进行了研究以解决我的问题,但似乎我缺乏异步功能。 我找到了这个解决方案,但它不起作用,我将在下面发布它: enter link description here

这是我的代码

 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
});

我正在尝试检查 Id 是否在我的数组中不存在,如果不存在只是通过 Id 不存在的错误,它工作正常,但它会在日志中保留打印错误说: TypeError: 无法在字符串上创建属性 'Symbol(level)' '在发送到客户端后无法设置标头' 感谢您的时间和合作

1 个答案:

答案 0 :(得分:0)

您不能发送多个回复。

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,
          },
        }
      );
    });
  }