I’m using Prisma with a MySQL database. This is my code:
const thisYear = Number("2023")
const newYear = thisYear + 1
const startDate = format(new Date(thisYear, 6, 1), 'yyyy-MM-dd HH:mm:ss')
const endDate = format(new Date(newYear, 5, 30), 'yyyy-MM-dd HH:mm:ss')
console.log("startDate, endDate: ", startDate, endDate)
const bucketTransactions = await prisma.bucket_transaction.groupBy({
by: ['bucket_id'],
_sum: {
amount: true,
},
where: {
date_added: {
gte: startDate,
lte: endDate,
},
amount: {
gt: 0,
},
donation_id: {
gt: 0,
},
},
});
I get this error:
Invalid value for argument `gte`: input contains invalid characters. Expected ISO-8601 DateTime.
This is the value of startDate and endDate in the console log:
startDate, endDate: 2023-07-01 00:00:00 2024-06-30 00:00:00
Do you know what’s wrong with my code?
I ran this directly into the SQL terminal and it worked:
select * from bucket_transaction WHERE date_added BETWEEN "2023-07-01 00:00:00" and "2024-06-30 00:00:00" AND amount > 0 AND donation_id > 0;
Why doesn’t it work with Prisma?
2
Answers
I had to change the startDate and the endDate into a new Date object like this:
You can change
to
or
Prisma work with the date directly or with the .toISOString()