windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

I am having trouble calling a product from the database. (MongoDb – Nodejs)

Asked By: Anonymous

When I typed phone in the search box, I found and fetched all categories with the word phone in the database. Then I wanted to find the products by matching the _id number of this category with the category id number of the product. but I cannot collect the products I find in a single array. that’s why I can’t print them all on the screen. Since two different arrays are created in the arrays, it prints the products in the first arrays, but does not pass to the second arrays.

array in array

As you can see from the picture, I cannot print it because the 3rd product is in the other array.

_x000D_

_x000D_

function escapeRegex(text) {
    return text.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&");
};

let model = [];
const searchRegex = new RegExp(escapeRegex(req.query.search), 'gi');
  SubSubCategory.find({ "name": searchRegex})
      .then(subsubcategoriesProduct => {
          subsubcategoriesProduct.forEach(p => {
              Product.find({ categories: p._id })
              .then(finalProduct => {
                  model.push(finalProduct);
                  res.render('shop/products', {
                      title: 'Tüm Ürünler',
                      products: model,
                      path: '/products',
                      searchRegex: searchRegex
                  });
...

_x000D_

_x000D_

x000D


Solution

Answered By: Anonymous

If there are 50 products in subsubcategoriesProduct, you are consequently launching 50 new Mongo queries at once inside the forEach. Each of these Product.find operations is asynchronous and will complete some time later, triggering 50 res.render. You can’t do that, you can only have one res.render.

Dealing with this kind of things using the traditional .then() syntax is complicated and easily leads to a callback hell (then inside then inside then). Using await instead of .then() makes things way easier.

Also, instead of making 50 queries (one for each _id) you should make one query with an array of _id.

const subcategories = await SubSubCategory.find({ name: searchRegex}, '_id')
                                        .lean() // return only JSON, not full Mongoose objects
                                        .exec(); // Returns a Promise so we can use await on it

const ids = subcategories.map(s => s._id);

const model = await Product.find({ categories: { $in : ids } }).lean().exec();

res.render('shop/products', {
                title: 'Tüm Ürünler',
                products: model,
                path: '/products',
                searchRegex: searchRegex
            });

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno