So I have a problem with mongoose in my NextJS, the problem is that I’m trying to iterate a set of categories and the product of each results to add onto the variable products, but is not working, when I console log inside the loop, the products are printed but not added in the array, and this is so weird.
import { Product } from "../../models";
import { Response } from "../../utils";
export default async function ProductsFilters(req, res) {
try {
const { Cities, Categories, Sort } = req.body;
let products = [];
if (Cities.length > 0) {
Cities.map(async (city) => {
const results = await Product.find({ City: city });
products.push(...results)
});
}
if (Categories.length > 0) {
Categories.map(async (category) => {
const results = await Product.find({ Category: category });
products.push(...results)
});
}
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);
}
}