I am filtering my items from the backend by query object which takes : brand,size,price.
Then I skip and limit ( pagination ) the list that I got.
Now the problem is when I want to filter by price in my app I got a slider which gets two values in array, example: [199,800] so I want to get all products between that range.
This is how I tried to do that:
getProductsPagination: async (
_,
{ page, productsFiltersInput: { size, brand, price, color } }
) => {
const query = {};
if (size) {
query.size = size;
}
if (brand) {
query.brand = brand;
}
if (color) {
query.color = color;
}
let result = Product.find(query);
let products;
const skip = (page - 1) * 12;
const limit = 12;
const totalItems = await Product.countDocuments(query);
const pages = Math.ceil(totalItems / limit);
result = result.skip(skip).limit(limit);
products = await result;
if (price.length > 0) {
products = products.filter(
(x) => x.price > price[0] && x.price < price[1]
);
}
return { products, numOfPages: pages };
},
The problem here as you can guess I am getting the products between that price range but I get them in seperate pages. I get 3 items in that range each item on each page which I don’t want to. I want them to be all in same page. Now I know what I am doing wrong but I am really don’t know how to continue from here. would like to get some hints!