Creating my first MERN application and connected the frontend to the backend.
Taking baby steps and trying to simply call the already existing “books” in my mongo database.
Heres what I have
const fetchingFunction = () => { fetch('http://localhost:5000/books', { method: "GET", headers: { "Content-Type": "application/json", 'Accept': 'application/json' } }).then(function(response) { response.json(); }).then(data => console.log(data)) }
I keep getting the error
Uncaught (in promise) SyntaxError: Unexpected token h in JSON at position 0
Advertisement
Answer
You need to return the response.json()
.
const fetchingFunction = () => { fetch('http://localhost:5000/books', { method: "GET", headers: { "Content-Type": "application/json", 'Accept': 'application/json' } }).then(function(response) { return response.json(); }).then(data => console.log(data)) }