JSON.parse unexpected error
I am making a MERN stack app where I have made a DB using Mongo Atlas and using node.js and express to fetch the data in my react app. I am calling the data using an
useEffect()
and awaiting the data as well. When I am trying to convert the data into json, it is giving me the error of
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
I am stuck here and cant solve this. I tried many different posts on net but none worked for me.
The backend server is running on localhost:4000
and react app is on localhost:5173
. I tried CORS
and even adding a proxy in package.json
but that also didnt work.
Here is my Github link:
https://github.com/Pulkit-s21/_mernStack/tree/main/_workoutApp
I would really appreciate any and all helpGitHub
_mernStack/_workoutApp at main · Pulkit-s21/_mernStack
Projects using MERN stack. Contribute to Pulkit-s21/_mernStack development by creating an account on GitHub.
10 Replies
How does the JSON response look like?
workoutSchema for mongodb
workoutController
useEffect on the Home Page
Are you using postman, insomnia or something like that?
99 times out of a hundred, that error means you're getting a 500 or 404/401 or some other error and the server is sending you HTML instead of JSON. Log the response body and see what's going on there
that's the response, not the response body
If I do
console.log(res.body)
it just shows that ReadableStream { locked: false }
try
console.log(await res.body)
?Same thing
I dont know what changed but now I am getting the
CORS
error and not the earlier oneThere's an express package called
cors
that you can use for that. https://expressjs.com/en/resources/middleware/cors.html