After a post request, the data gets inside the database but it is not re rendering the component. I need to manually press the F5 button and the new data will be displayed. It seems like the then
is not executing.
class AddForm extends Component {
constructor(props) {
super(props)
this.state = {
id: null,
title: "",
price: 0,
pages: 0,
};
}
postData() {
const { title, price, pages } = this.state
const curTitle = title
const curPrice = price
const curPages = pages
fetch("api/books", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
Title: title,
Price: price,
NumberOfPages: pages
})
}).then(res => res.json()).then(() => this.setState({ title: curTitle, price: curPrice, pages: curPages }))
}
}
Edit: When I do the following:
.then(response => response.json()).then(res => console.log(res))
I get Uncaught (in promise) SyntaxError: Unexpected end of JSON input