I am trying to apply filters by adding them into query like this
http://localhost:3000/?bathroomCount=1&endDate=2024-06-14T17%3A06%3A17%2B05%3A30&guestCount=1&roomCount=1&startDate=2024-06-14T17%3A06%3A17%2B05%3A30
But i am not getting the results on my webpage
// This is what i was trying to expect
https://youtu.be/c_-b_isI4vg?si=php_lZJ_e_nwrnCZ&t=30051
// This is what i get
No Filters are applied in the results
enter image description here
// Here is the code
https://github.com/Anmol-sudo/airbnb-clone/blob/fix-list-filtering-error/app/actions/getListings.ts
The getListings function is not working as usual
import prisma from "@/app/libs/prismadb";
export interface IListingsParams {
userId?: string;
guestCount?: number;
roomCount?: number;
bathroomCount?: number;
startDate?: Date;
endDate?: Date;
locationValue?: string;
category?: string;
}
export default async function getListings(params: IListingsParams) {
try {
const {
userId,
roomCount,
guestCount,
bathroomCount,
locationValue,
startDate,
endDate,
category,
} = params;
let query: any = {};
if (userId) {
query.userId = userId;
}
if (category) {
query.category = category;
}
if (roomCount) {
query.roomCount = {
gte: +roomCount,
};
}
if (guestCount) {
query.guestCount = {
gte: +guestCount,
};
}
if (bathroomCount) {
query.bathroomCount = {
gte: +bathroomCount,
};
}
if (locationValue) {
query.locationValue = locationValue;
}
if (startDate && endDate) {
query.NOT = {
reservations: {
some: {
OR: [
{
endDate: {gte: startDate},
startDate: {lte: startDate},
},
{
startDate: {lte: endDate},
endDate: {gte: endDate},
}
]
}
}
}
}
const listings = await prisma.listing.findMany({
where: query,
orderBy: {
createdAt: "desc",
},
});
const safeListings = listings.map((listing) => ({
...listing,
createdAt: listing.createdAt.toISOString(),
}));
return safeListings;
} catch (error: any) {
throw new Error(error);
}
}
Krishna Weber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.