0

I have this express POST route.Which i'm hitting with the help of jQuery using axios.

app.post("/user", jsonParser, (req, res) => {
  const requrl = req.body.url;
  const URL = extractprofile(requrl);
  const shorturl = URL.shorturl;
  const longurl = URL.longurl;
  const regex = /^[0-9]*$/i;
  const matches = regex.exec(shorturl);
  if (matches === null) {
    getSteamID64(longurl)
      .then(function (result) {
        const steam64 = result;
        **const data = playerInfo(steam64)**;
         console.log(data) // undefined
      })
      .catch(function (error) {
        console.log(error);
      })
  } else {
    const steam64 = shorturl
    **const data = playerInfo(steam64)**;
    console.log(data);        //undefined
  }
})

Lets just focus on const data = playerInfo(steam64).The code for the function playerInfo(steam64) is below:

function playerInfo(steam64) {
  SteamApi.getPlayerInfo(steam64, "D295314B96B79961B1AB2A2457BA5B10", (err, data) => {
    if (err) {
      console.log("error", err);
    } else {
      console.log(data);
      return data
    }
  })
}

This function does a API call to steam and gets the playerinfo.Whose profile link we are passing as a parameter to the function. I guess the problem is asynchronosity of javascript. Before the function returns the value. console.log(data) // In the post route is executed so it is console logging undefined.

I tried this :

    async function playerInfo(steam64) {
  let response
  try {
    response = await SteamApi.getPlayerInfo(steam64, "D295314B96B79961B1AB2A2457BA5B10",(err, data) => {
    if (err) {
      console.log("error", err);
    } else {
      console.log(data);
    });
  }
  catch (error) {
    console.log(error);
  }
  return response;
}

  //POST ROUTE 

playerInfo(steam64).then(steamdata => {
  console.log(steamdata);  //undefined
});

The above async function refractor also doesnt work.VSCODE keeps telling me 'await has no effect in this type of expression'.And surely even after this it console logs 'undefined'.

It works when i replace the 'playerInfo(steam64)' function call with the entire function.But the POST route will become too much clumsy.I want to divide the code into functions. Any help is appreciated.The goal is to run playerInfo(steam64) in the post route.And make the playerInfo(steam64) function to return the result.

Sai Darshan
  • 252
  • 3
  • 13
  • See [How do I convert an existing callback API to promises?](https://stackoverflow.com/q/22519784/1048572) if you want to use `await` and return something. – Bergi May 06 '20 at 10:03

2 Answers2

0

That is your code:

function playerInfo(steam64) {
  SteamApi.getPlayerInfo(steam64, "D295314B96B79961B1AB2A2457BA5B10", (err, data) => {
    if (err) {
      console.log("error", err);
    } else {
      console.log(data);
      return data
    }
  })
}

playerInfo() is returning undefined because it has no return statement.

the return statement is withing this anonymous function:

(err, data) => {
    if (err) {
      console.log("error", err);
    } else {
      console.log(data);
      return data
}

maybe this is where you go wrong.

Buntel
  • 540
  • 6
  • 19
0

You have to decide if you want to use it with callbacks or with Promises.

Using callbacks

Add a callback param to the function:

function playerInfo(steam64, callback) {
  SteamApi.getPlayerInfo(steam64, "D295314B96B79961B1AB2A2457BA5B10", (err, data) => {
    if (err) {
      console.log("error", err);
    } else {
      console.log(data);
    }
    callback(err, data);
  })
}

Then use the callback:

playerInfo(steam64, (err, steamdata) => {
  console.log(steamdata);
});

Using Promises

Maybe SteamApi already has a Promise interface, if that is the case, you could use it directly.

If it does not, then convert your call to SteamApi from a callback to a Promise and return that Promise:

function playerInfo(steam64) {
  return new Promise((resolve, reject) => {
    SteamApi.getPlayerInfo(
      steam64,
      "D295314B96B79961B1AB2A2457BA5B10",
      (err, data) => {
        if (err) {
          console.log("error", err);
          reject(err);
        } else {
          console.log(data);
          resolve(data);
        }
      }
    );
  });
}

Then use it as a Promise as usual, with then:

playerInfo(steam64).then(steamdata => {
  console.log(steamdata);
});

or inside an async function:

async function() {
   // ...
   const steamdata = await playerInfo(steam64);
   console.log(steamdata);
   // ...
}
Ignacio Lago
  • 2,432
  • 1
  • 27
  • 33