P
Prisma4mo ago
sommeeer

Returning the next day closest to NOW

So I have a list of dates, and im trying to return the next date that is closest to new Date.now(); Here is the code:
const course = await db.course.findFirst({
where: {
id: params.id,
},
include: {
courseDate: {
where: {
from: {
gte: new Date(),
},
},
take: 1,
},
},
});
const course = await db.course.findFirst({
where: {
id: params.id,
},
include: {
courseDate: {
where: {
from: {
gte: new Date(),
},
},
take: 1,
},
},
});
This doesnt work it returns :
[
{
id: 10,
from: 2024-04-13T10:00:00.000Z,
to: 2024-04-13T14:00:00.000Z,
courseId: 8
}
]
[
{
id: 10,
from: 2024-04-13T10:00:00.000Z,
to: 2024-04-13T14:00:00.000Z,
courseId: 8
}
]
This is my entire courseDate array: https://bpa.st/NK6Q - as you can see there the closest date from now(2024-04-12T06:04:45.733Z) should be the courseDate with id: 21. Anyone know what it might be ? 🙂
1 Reply
sommeeer
sommeeer4mo ago
alright i figured it out, i had to do an orderBy on the from column in ascending order. can anyone confirm that this is the solution?