Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When i push value to empty array it showing Node js

exports.OrderPlace = (req, res) => {
  const all_product = req.body.product_info;
  let meta = [];
  all_product.forEach((element) => {
    Ticket.find({ _id: element.product_id }, function (err, docs) {
      if (err) return handleError(err);
      if (docs[0]._id == element.product_id) {
        if (element.quantity < docs[0].quantity) {
          meta.push({
            cart_id: element.id,
            pro_id: element.product_id,
            quantity: element.quantity + " Asking quentity is not available!",
          });
        }
      }
    });
  });
  console.log(meta);
};

I’m trying to push cart_id , pro_id, quantity. its loging me empty value please help

Im expecting console.log(meta) values like

[
    {
        cart_id: "63db8665ba7126c2b35fb231",
        pro_id: "63d025a8eefcf49cdcdd5472",
        quantity: "36 Asking quentity is not available!",
    },
    {
        cart_id: "63dbc2a7fbf7daf48052189e",
        pro_id: "63ce4393c3433881173d1502",
        quantity: "40 Asking quentity is not available!",
    }
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

wrap the whole code block inside an async function, and use await inside the function to wait for the result of the Ticket.find operation.

exports.OrderPlace = async (req, res) => {
  const all_product = req.body.product_info;
  let meta = [];
  let flag = "";

  for (const element of all_product) {
    const docs = await Ticket.find({ _id: element.product_id }).exec();
    if (docs[0]._id == element.product_id) {
      if (element.quantity > docs[0].ticket_quantity) {
        flag = "false";
        meta.push({
          cart_id: element.id,
          pro_id: element.product_id,
          quantity: element.quantity + " Asking quentity is not available!",
        });
      }
    }
  }
  console.log({ flag: flag, meta });
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading