0

I want to dynamically grab the ip adress from this device (since it changes overtime) and then fetch the JSON from the database by using a link in fetch: https://{ipadress}:5000/users from node.js server. But now it gives me an error Can not use keyword 'await' outside an async function. I don't know how to solve it. The function publicIP() is where the problem is at.

async componentDidMount() {
            this.drawOnCanvas();
            var ipAdress = '';
            publicIP()
            .then(ip => {    
                console.log(ip);
                const url = "http://" + ip + ":5000/user";
                const response = await fetch(url);
                const data = await response.json(); 
                this.setState({userData: data});
                console.log(this.state.userData);
            // '47.122.71.234'
            })
            .catch(error => {
            console.log(error);
            // 'Unable to get IP address.'
            });
           

2 Answers 2

2

You shouldn't mix async/await with .then().catch(). Select the one you like better and stick with it. So in your case You should either do

componentDidMount() {
  this.drawOnCanvas();
  var ipAdress = '';
  return publicIP()
    .then(ip => {    
      console.log(ip);
      const url = "http://" + ip + ":5000/user";
      return fetch(url);
    })
    .then(res => {
      return res.json();
    })
    .then(data => {
      this.setState({userData: data});
      console.log(this.state.userData);            
    })
    .catch(error => {
       console.log(error);
    });
}

or

async componentDidMount() {
  try {
    this.drawOnCanvas();
    var ipAdress = '';
    const ip = await publicIP()
    console.log(ip);
    const url = "http://" + ip + ":5000/user";
    const response = await fetch(url);
    const data = await response.json(); 
    this.setState({userData: data});
    console.log(this.state.userData);
  } catch (error) {
    console.log(error);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

In the first code example, is the return before publicIP() needed?!
@Yousaf Depends. In case of a react componentDidMount() probably not (I'm not very familiar with react, so I may be wrong here). In general case if you want some method xy() to be part of a promise chain, yes, because otherwise this won't wait for the promise to resolve/reject
componentDidMount is a lifecycle method, that return is not needed here.
1

Try it like this:

async componentDidMount() {
    this.drawOnCanvas();
    try
    {
        var ip = await publicIP();
        console.log(ip);

        const url = "http://" + ip + ":5000/user";
        const response = await fetch(url);
        const data = await response.json(); 
        this.setState({userData: data});
        console.log(this.state.userData);  // '47.122.71.234'
    } catch(error) {
        console.log(error);  // 'Unable to get IP address.'
    }
}

And additionally I think you should use either async/await OR .then/.catch but don't mix them.

5 Comments

Thanks! it worked. For some reason i get the wrong ip though, its different from when i type in command prompt ipconfig and go to ipv4. Any ideas to get the correct IP? So i can fetch it
It seems that you want to call an endpoint on your local machine, right? So why can't you just use the fixed value 127.0.0.1 or localhost as your ip?
Well, I want to connect to this app from other computers as well. Because of that I need the local ip of the computer where i run the react app on. So that other computers can also fetch the json data
OK but how should the other computers know the ip address of your host that is running your react app? I think you have to hard code the address in your consumers or do some routing (like editing hosts config to map an ip address to a dns name).
And as far as I know React is just a client side technology. So you starting a local server on the machine which wants to use this frontend. This server just serves the JS and HTML files for the frontend. There is no backend service involved here. If you want to call a backend to fetch some data you need a backend technology as well (NodeJS, Java, ...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.