Reputation: 31
I am trying to add data to my database. Upon execution of my fetch method I receive the following error
Uncaught (in promise) SyntaxError: Unexpected end of JSON input Promise.then (async)
I have tried re-configuring the promise but am unsure if I am doing this correctly.
addNewBeer() {
fetch("https://beer.fluentcloud.com/v1/beer/", {
body: "{\"name\":\"\",\"likes\":\"\"}",//input beer name and like amount ex "{\"name\":\"Michelob Ultra\",\"likes\":\"-5\"}"
headers: {
"Content-Type": "application/json"
},
method: "POST"
})
.then(response => response.json())
.then(responseJson => {
this.setState({
isLoaded: true,
dataSource: this.state.dataSource + responseJson,
});
})
}
Upvotes: 2
Views: 4408
Reputation: 31595
This error Uncaught (in promise) SyntaxError: Unexpected end of JSON input Promise.then (async)
happens when the response you get isn't a JSON. The error happens in this part:
.then(response => response.json())
If you omit that part, you will see that res
isn't a JSON, probably a string.
You can try to do a console.log
to see what is the response and then provide that in your question.
Upvotes: 3