1
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!",
    }
]
K2 G
  • 23
  • 4
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – David Feb 03 '23 at 12:28
  • No, I have a different scenario. I want to push unavailable data to meta variable – K2 G Feb 03 '23 at 12:32
  • 2
    No, you have the same scenario. `Ticket.find()` is an asynchronous operation (which is why it requires a callback function). If there's a Promise-based version of that functionality, that would be easier to perform in a loop with `await`. Failing that, there are other approaches you can take. But overall what you're trying to do is perform an asynchronous operation and get its result, which is exactly what the linked duplicate covers in significant detail. – David Feb 03 '23 at 12:34
  • I'm unable to think – K2 G Feb 03 '23 at 12:41
  • 1
    Adding to @David 's response. The `async` call won't work properly with `array.forEach()`, you'll need a traditional `for` loop and make the `OrderPlace` function to be `async` – Batman Feb 03 '23 at 12:52

2 Answers2

1

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

The easiest way that I found to manipulate the array of objects is using the array methods like filter, map etc...

Maybe following code helps you

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

      element = element.filter((item) => {
          return (docs[0]._id == element.product_id && element.quantity < docs[0].quantity)
      })

      meta = element.map((item) => {
            return {
                cart_id: element.id,
                pro_id: element.product_id,
                quantity: element.quantity + " Asking quentity is not available!",
            }
      });
    });
  });
  console.log(meta);
};
Dhaval
  • 868
  • 12
  • 22