0

I'm using the Steam Developer API to create a Discord Bot. I'm using the ResolveVanityURL call of ISteamUser, and it is calling correctly. However, when I try to use it within my code, it always states "response is not defined". The output from the call is very simple and is as follows:

enter image description here

My snippet of code is as follows:

let { vanityURL } = superagent
            .get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);
        console.log(vanityURL.response.steamid)

Essentially, I'm calling the API using arguments from my Discord bot to fetch the Steam ID from the Vanity URL. It calls without errors, but when I try console.log it as seen above, it gives me this error: (node:14580) UnhandledPromiseRejectionWarning: ReferenceError: response is not defined.

How can I make it so this error doesn't continue? Thanks.

cyliim
  • 39
  • 7
  • 1
    `superagent.get(url)` is a promise, so you need to await it, but what's the actual result of that call? I saw the image but it could mean two things, `{ steamid: "id_here", success: 1 }` or `{ response: { steamid: "id_here", success: 1} }` so which is it?` –  Jun 27 '20 at 04:57
  • the second one, having steamid and success under response – cyliim Jun 27 '20 at 05:01
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ABGR Jun 27 '20 at 05:46

2 Answers2

1

You should use the promise to get the response:

let { vanityURL } = superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`)
        .then(res => console.log(res));

note the use of then to resolve the promise after using get.

dacuna
  • 1,096
  • 1
  • 11
  • 22
1

Firstly superagent.get() is async so you need to add await if you want to directly assign a variable to the response. With your current code its trying to deconstruct vanityURL from a promise, which is obviously undefined

let { vanityURL } = await superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);

Second, where did vanityURL come from? Is it another property of the call? Either way your steamid is inside of response property so you will need to deconstruct that:

let { response, vanityURL } = await superagent.get(`http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=${stApiKey}&vanityurl=${args[0]}`);

After that you can just call .steamid on the response:

console.log(response.steamid);