joschan21 / nextjs-realtime-chat

A performant and reliable realtime-chat-app built with NextJS 13
906 stars 158 forks source link

The error message we are receiving suggests that the string you are attempting to parse as JSON is not valid JSON #6

Open SushekTamrakar opened 1 year ago

SushekTamrakar commented 1 year ago

It says "Unexpected token u" in browser error console. The "Unexpected token u" error typically occurs when you try to parse a string that is undefined or null, because the first character of undefined or null is "u".

In your code, it's possible that the lastMessageRaw variable is undefined or null, which is causing the error when you try to parse it as JSON. You can add a null check to make sure that lastMessageRaw has a value before attempting to parse it as JSON.

https://github.com/joschan21/nextjs-realtime-chat/blob/78057985c9cb588afb3745fa52ece5c435dcd075/src/app/(dashboard)/dashboard/page.tsx#L19-L31

eltel commented 1 year ago

I fixed this with the following: const lastMessage = lastMessageRaw ? (JSON.parse(lastMessageRaw) as Message) : "";

Hope that helps.