question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTime values wrongly typed in Prisma Client

See original GitHub issue

Bug description

When using start as an ISO string in a date library, I get this error. There are no runtime errors as it’s actually a string:

// Argument of type [see below] is not assignable to parameter of type 'string'
DateTime.fromISO(start)

Prisma schema

start          DateTime

Lets console.log(start)

1970-01-01T00:00:00.000Z

TypeScript type for start

start: {
    toString: () => string;
    toDateString: () => string;
    toTimeString: () => string;
    toLocaleString: {
        (): string;
        (locales?: string | string[] | undefined, options?: Intl.DateTimeFormatOptions | undefined): string;
    };
    ... 40 more ...;
    [Symbol.toPrimitive]: {
        ...;
    };
}

Prisma Version

prisma                  : 3.11.1
@prisma/client          : 3.11.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 1a2506facaf1a4727b7c26850735e88ec779dee9 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 1a2506facaf1a4727b7c26850735e88ec779dee9 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 1a2506facaf1a4727b7c26850735e88ec779dee9 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 1a2506facaf1a4727b7c26850735e88ec779dee9 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 1a2506facaf1a4727b7c26850735e88ec779dee9
Studio                  : 0.458.0
Preview Features        : fullTextSearch

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
marc-on-githubcommented, Mar 31, 2022

I just have the same problem

1reaction
aqrlncommented, May 11, 2022

@ekerik220 in your case Prisma returns a Date. It is then lost and converted to a string due to serialization to JSON by Remix. You can see what’s happening by adding some logs on the server side as well.

Logs:

createdAt type in getNote: object
createdAt instanceof Date: true
createdAt data type in component: string

Diff:

diff --git a/app/models/note.server.ts b/app/models/note.server.ts
index ba56b53..4f23cc0 100644
--- a/app/models/note.server.ts
+++ b/app/models/note.server.ts
@@ -4,15 +4,18 @@ import { prisma } from "~/db.server";
 
 export type { Note } from "@prisma/client";
 
-export function getNote({
+export async function getNote({
   id,
   userId,
 }: Pick<Note, "id"> & {
   userId: User["id"];
 }) {
-  return prisma.note.findFirst({
+  const note = await prisma.note.findFirst({
     where: { id, userId },
   });
+  console.log("createdAt type in getNote:", typeof note?.createdAt);
+  console.log("createdAt instanceof Date:", note?.createdAt instanceof Date);
+  return note;
 }
 
 export function getNoteListItems({ userId }: { userId: User["id"] }) {
diff --git a/app/routes/notes/$noteId.tsx b/app/routes/notes/$noteId.tsx
index b326f37..da07201 100644
--- a/app/routes/notes/$noteId.tsx
+++ b/app/routes/notes/$noteId.tsx
@@ -36,7 +36,7 @@ export default function NoteDetailsPage() {
   const data = useLoaderData() as LoaderData;
 
   // Typescript says createdAt is a Date, but the console log says it's a string.
-  console.log("createdAt data type:", typeof data.note.createdAt);
+  console.log("createdAt data type in component:", typeof data.note.createdAt);
 
   return (
     <div>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error message reference - Prisma
Prisma Client throws a PrismaClientInitializationError exception if something goes wrong when the query engine is started and the connection to the database ...
Read more >
Prisma Schema not updating properly after adding new fields
It seems like the operation is using a previous version of the schema seeing as how it says that the email field does...
Read more >
prisma datetime null - You.com | The Search Engine You Control
Prisma 2 unable to fetch DateTime column with null value ... Value out of range for the type. ... Prisma client not marking...
Read more >
Prisma 3.14.0 Release - GitClear
Prisma Client queries such as findMany deserialize database scalar values to their corresponding JavaScript types. For example, a DateTime ...
Read more >
It's Prisma Time - Create Table - DEV Community ‍ ‍
The published property is of type Boolean and its default value is false; The createAt is of type DateTime and when a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found