Skip to content
loading...

Huge problem: code does not go further than await

dieldore profile image Dieldore github logo ・1 min read

Code in App.js

componentDidMount() {
    this.getAccountData();
}
async getAccountData() {
    try {
      let FD = new FormData();
      FD.append('test', true);
      let Q = fetch('/ajax/get', {
        method: 'POST',
        body: FD
      });
      let data = await Q.json();
      console.log(data);
      this.setState({
        id: data.id,
        name: data.name
      });
    }
    catch(e) {
      console.log('error');
    };
  }

After await is called, it gets the data in JSON. But if you put console.log after it, then they do not work, so setState also breaks my application. debugger also does not work there.
I am using firefox 68.6 and 80.0.3987.132

twitter logo DISCUSS (2)
Discussion
markdown guide
 

You have to await Q and then execute .json() on the result. fetch() returns a promise that can be awaited.

Would probably go for something like:

 let Q = await fetch('/ajax/get', {
  method: 'POST',
  body: FD
});
let data = await Q.json();

Read 4th parahraph under Concepts and usage:
developer.mozilla.org/en-US/docs/W...

 

Thank you, just noticed. In a hurry forgot to specify await

Classic DEV Post from Jul 30 '19

PublishTo.Dev: Scheduling article publishing on dev.to

Dieldore profile image