2

Hi i have just started to learn node.js and javascript. And I'm trying to fetch data from a url using node-fetch and then trying to json parse the data but i keep getting a return undefined.

I'm trying to get the data from the variable game from the html below and then stringify and then parse the json but it returns undefined.

And i was hoping maybe someone could help me figure out why?

The html code is not mine i am trying to fetch data from a site i don't control.

Page html:

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8" />
    <title>Document Title</title>
  </head>
  <body>
    <div id="hud-div">
      <p>Welcome to the blah blah blah blah text here</p>
    </div>
    <script>
      var game = new Game({
        stage: "prod",
        servers: {
          v32306117: {
            id: "v32306117",
            region: "region 1",
            name: "region #1",
            hostname: "hostname1",
            port: 80,
            fallbackPort: 8000,
            population: 97,
            selected: false
          },
          v32306125: {
            id: "v32306125",
            region: "region 2",
            name: "region #2",
            hostname: "hostname2",
            port: 80,
            fallbackPort: 8000,
            population: 38,
            selected: false
          }
        },
        userGroup: 0
      });

      game.init(function() {
        game.assetManager.load([{
          "name": "\/asset\/image\/map\/grass.png",
          "url": "\/asset\/image\/map\/grass.png"
        }]);

        game.debug.init();
        game.run();
      });

    </script>
  </body>
</html>

Fetch function:

    const fetch = require("node-fetch");

    async function serversFetch() {
      try {
        const servers = await fetch("https://get-servers.herokuapp.com/");
        const data = await servers.text();
        const servers_data = data.substring(
          data.lastIndexOf("var game =") + 20,
          data.lastIndexOf("game.init") - 10
        );

        return JSON.stringify(servers_data);
      } catch (error) {
        console.log(error);
      }
    }

    (async () => {
      const data = await serversFetch();
      console.log('data', data);

      const info = JSON.parse(data);
      console.log('info', info.servers); // returns undefined
    })()

if i console log info without servers i get a response like below but when i try to console log info.servers it comes back undefined.

info {
        stage: "prod",
        servers: {
          v32306117: {
            id: "v32306117",
            region: "region 1",
            name: "region #1",
            hostname: "hostname1",
            port: 80,
            fallbackPort: 8000,
            population: 97,
            selected: false
          },
          v32306125: {
            id: "v32306125",
            region: "region 2",
            name: "region #2",
            hostname: "hostname2",
            port: 80,
            fallbackPort: 8000,
            population: 38,
            selected: false
          }
        },
        userGroup: 0
      }

I have made an example site using herokuapp for anyone that wants to help.

1 Answers1

1

I would recommend against mixing async/await and .then. Here is a a method of working with awaits to get at the data.

        const servers = await fetch("https://get-servers.herokuapp.com/");
        const data = await servers.text();
        // Work with data logic
jasonandmonte
  • 1,869
  • 2
  • 15
  • 24