#VercelKV db doesn't fetch correct types of data.

1 messages · Page 1 of 1 (latest)

daring temple
#

Hey guys, I have this weird functionality that hasn't been a thing up until this point and it's the fact that fetching doesn't respect data types as shown in the example below. Unfortunately, this leads to errors and undefined behavior in my code.

This is actually the nextjs template, that if you download and use update it's packages it would work, especially at the strict equalities because of the type ( === and !== ) .

export interface Chat extends Record<string, any> {
  id: string
  title: string
  createdAt: Date | number
  userId: string
  path: string
  messages: Message[]
  folder: string | null
  sharePath?: string
}

export async function getSharedChat(id: string) {
  const chat = await kv.hgetall<Chat>(`chat:${id}`)
  console.log (typeof chat?.createdAt, chat!.createdAt); //outputs: string 1701006596925
torpid shoalBOT
#

Hi @daring temple. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems like you're encountering an issue with data serialization in Next.js. However, according to the Next.js documentation (https://nextjs.org/docs/app/building-your-application/configuring/typescript), as of Next.js 13, there is no need to serialize data between the fetching function and the page. This means you can fetch directly in components, layouts, and pages on the server, and the data does not need to be serialized (converted to a string) to be passed to the client side for consumption in React.

Here's an example from the documentation:

async function getData() {
 const res = await fetch('<https://api.example.com/...'>)
 // The return value is *not* serialized
 // You can return Date, Map, Set, etc.
 return res.json()
}
 
export default async function Page() {
 const name = await getData()
 
 return '...'
}

In this example, the return value from the fetch call is not serialized, meaning you can return complex data types like Date, Map, Set, etc. without any extra steps.

However, it's important to note that this feature is available when using Server Components in Next.js. If you're not using Server Components, you might still encounter issues with data serialization.

If you're still encountering issues, it might be helpful to provide more context or check the official Next.js GitHub issues for similar problems.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: