wedmaniac
wedmaniac4w ago

Parsing a fetch response resulting number turning into NaN

Hi there! I'm making a website where I try to gameify the experience. However now im stuck with a problem! So I fetch energy from my supabase table. So far so good. but when I parse the response one field ("balance") gets turned into NaN!
const fetchEnergy = async () => {
const response = await fetch("/api/get/energy", { method: "GET" });
const rawResponse = await response.text();
console.log("Raw API response:", rawResponse);

const data = JSON.parse(rawResponse);
console.log("Parsed API response:", data);
....
const fetchEnergy = async () => {
const response = await fetch("/api/get/energy", { method: "GET" });
const rawResponse = await response.text();
console.log("Raw API response:", rawResponse);

const data = JSON.parse(rawResponse);
console.log("Parsed API response:", data);
....
I have no idea how to resolve the issue here! Thanks for the help in advance ❤️
No description
11 Replies
Doctor 🤖
Doctor 🤖4w ago
That does appear to be odd
wedmaniac
wedmaniacOP4w ago
So im not crazy:P I feel like im going insane!
Doctor 🤖
Doctor 🤖4w ago
My only guess would be that it's not actually a 5 but some other character that looks like a 5 Or maybe there is an invisible character next to it screwing up the parsing
wedmaniac
wedmaniacOP4w ago
It is a 8 byte int so im pretty certain its a 5:)
Niterux
Niterux3w ago
i might be a little obtuse but have you tried just directly using await response.json()
Niterux
Niterux3w ago
MDN Web Docs
Response: json() method - Web APIs | MDN
The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON.
Niterux
Niterux3w ago
other than that maybe the json parser has a bug in it
Ewok
Ewok3w ago
or do you change the data object after because the console updates it @wedmaniac
wedmaniac
wedmaniacOP3w ago
Hi, thx for the reply ! Yes. I have tried that!;) No sir! I was really baffled so I had to change direction! Now I include the data in the context!
Ewok
Ewok3w ago
Hmm... I'd still log data.userCurrency.balance just to narrow things down
wedmaniac
wedmaniacOP3w ago
Maby I encounter it in the future:)