0

I receive an array of products that I need to search for with Mongoose, so I use Find passing the _id of each product. But when going through the array and trying to search, I only receive the data of the first product, for the others I always receive undefined.

This is my code:

const calc = async (details) => { 
    
    let grandSubtotal = 0; }
    console.log(details);
    for (let i = 0; i < details.length; i++) {
        let verifyProduct = await Product.find({ _id: details[i]._id});
        console.log(verifyProduct[i].salePrice);  //It only shows the first product, the others are undefined

.......

    }

In my MongoDB database I have the products all saved with salePrice always, in this way:

{
  "_id": "628fa841cde1d960c675ee24",
  "barCode": "0000075053765",
  "idProduct": "03",
  "name": "MALBORO ARTESANAL 20",
  "desc": "PAQUETE 20 CIGARROS",
  "presentation": "PIECES",
  "salePrice": 550,
  "purchasePrice": 526,
  "stock": 0,
  "available": true,
  "img": [],
  "status": false
}

How can I obtain the salePrice information of all the products that I receive since for now I only receive information on the first one and the others are always undefined?

2 Answers 2

1

this because, you are using .find({})

let verifyProduct = await Product.find({ _id: details[i]._id});

You're querying using .find({ _id: details[i]._id}), you will always have result in the form of [{..onevalue at 0..}] because .find() returns result in an []

so, when you execute the loop for the first time, your i will be 0 and so when you access the verifyProduct[0].salePrice it will have value. But when your i become 1, your verifyProduct will still have result at 0 position only.

Fix:

const calc = async (details) => {
  let grandSubtotal = 0;
  console.log(details);
  for (let i = 0; i < details.length; i++) {
    let verifyProduct = await Product.findById(details[i]._id);
    // no array, so access it directly
    console.log(verifyProduct.salePrice);
  }
}

since you are querying by _id, you can use .findById({}) instead of .find().

0
0

Database query in a for loop is super inefficient. You should use the $in MongoDB operator to select multiple documents at once.

Example

const arrayOfIds = ["631318a217f73aa43a58855d", "63132ba7525da531e171c964"];

Product.find({ _id: { $in: arrayOfIds }});

3
  • The problem with $in, in my case, is that I don't just get an array of ids but I get an array with objects inside with different values
    – OscarDev
    Sep 3, 2022 at 22:34
  • Use the map function to create array of ids seems to be cleaner solution for me. const arrayOfIds = originalArray.map(product => product._id)
    – simecek
    Sep 4, 2022 at 10:41
  • Yes, I did that but I noticed that it gives me an array again, I mean that $in again I have an array. So I don't know how convenient it is to be looping through arrays or using filter of two arrays. I don't know, but I think it's more complex.
    – OscarDev
    Sep 5, 2022 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.