2

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

Giga Meta
  • 299
  • 2
  • 16

3 Answers3

1

Well from your question it looks like that you are able to make the state update on submitting, the only problem is regarding displaying the data, for that I may suggest you to put another component for displaying the data. It had worked for me in the past. Just let me know whether it worked for you.

rohitmi0023
  • 109
  • 1
  • 2
  • 12
0

res => res.json(): this gives a JSON response, bind this JSON response values to the state in the setState.

Hoping your response will be like below,

response.data = {
   curTitle: 'some title',
   curPrice: 'some price',
   curPages: 'some pages'
}

Change your fetch call second then function like below,

constructor(props) {
   super(props)
   this.state = {
      id: null,
      title: "",
      price: 0,
      pages: 0,
   };
   this.postData = this.postData.bind(this); // Add this line, cannot read property error will be gone.
}

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((responseJson) => {
  console.log(responseJson); // what is the output of this?
  this.setState({
    title: responseJson.data.curTitle,
    price: responseJson.data.curPrice,
    pages: responseJson.data.curPages
  })
})
Gangadhar Gandi
  • 2,162
  • 12
  • 19
-1

You should create new function which get from back end. Then call this function after post respons

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 09 '23 at 18:06