Hi, this might be noob-question, but maybe you peo...
# orm-help
p
Hi, this might be noob-question, but maybe you people can give me some guidance: I’m fetching a list of “groups” which includes different stuff like a name, type, other stuff, and then lastly a “date” (coming from a DateTime in the schema). Now, when I’m sending this response, the date is a
string
and not a
date
. I know I can create a
New Date()
on the frontend but my types are messing up because it’s set to
Date
-type in the schema but it’s actually a string in my code. What is the correct way to handle this?
👀 1
n
Hey 👋 If I understand correctly, you mean that from the frontend you are passing a date string, but in the schema file the field is defined as a Date type. I am assuming that you are having issues inserting the date field. (Let me know if that’s not the case). You could pass the string which is obtained from the frontend to the date object like this:
new Date('2020-01-01T00:00:00.000Z')
So considering this model:
Copy code
model User {
  id        String   @id @default(cuid())
  firstName String
  lastName  String
  email     String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  userTime  DateTime
}
You could insert the record like this:
Copy code
const createUser = await prisma.user.create({
    data: {
      email: '<mailto:test@test.com|test@test.com>',
      firstName: 'John',
      lastName: 'John',
      createdAt: new Date(),
      userTime: new Date('2020-01-01T00:00:00.000Z'),
    },
  });
p
@Nurul Thank you so much for answering! It’s actually the opposite way around. When I’m getting the date from the DB the type is a string instead of a Date when passing it as a response to my frontend - am I doing something wrong maybe?
n
Ideally date should be stored as a DATE type in database, can you change the column in database to be of date type?