0

So, I have the array of products that I need to fill, but something is not working when I'm iterating the Categories and Cities to fill the promises and then setting all the products to the variable, but something is not working?

import { Product } from "../../models";
import { Response } from "../../utils";

export default async function ProductsFilters(req, res) {
  try {
    const { Cities, Categories, Sort } = req.body;

    let products = [];
    let promises = [];

    if (Cities.length > 0) {
      Cities.map(async (city) => {
        const results = await Product.find({ City: city });
        promises.push(results);
      });
    }

    if (Categories.length > 0) {
      Categories.map(async (category) => {
        const results = await Product.find({ Category: category });
        promises.push(results);
      });
    }

    await Promise.all(citiesPromises).then((res) => console.log(res))

    if (products) Response( res, 200, true, "Të gjitha produktet u morën me sukses.", products);
    else Response(res, 404, false, "Asnjë produkt nuk u gjet në platformë.", null);
  } catch (error) {
    Response( res, 500, false, "Gabim i brendshëm i serverit gjatë gjetjes së produkteve.", null);
  }
}
nevoni3008
  • 227
  • 1
  • 11

0 Answers0