I'm trying to write a library for Riot (League of legends API) in NodeJS and I have the following problem.
I'm doing:
function getSummonerProfile(sum) {
var summoner = sum.replace(/\s+/g, '');
request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"
+ summoner + "?api_key=" + api_key, function(error, response, body) {
console.log(body);
});
}
getSummonerProfile("some player");
the console shows:
{
"player": {
"id": 37842773,
"name": "player",
"profileIconId": 548,
"summonerLevel": 30,
"revisionDate": 1368783726000
}
}
Now "player" is a variable (function parameter); how can I access the data? to get for example, only the id.
body.summonner and body["player"] throw undefined.
EDIT 1 (complete code):
var request = require("request");
var api_key = 'example';
function getSummonerProfile(sum) {
var summoner = sum.replace(/\s+/g, '');
request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"
+ summoner + "?api_key=" + api_key, function(error, response, body) {
console.log(body.summoner.id);
});
}
getSummonerProfile("some player");
Your function getSummonerProfile only sends a request, using this request function which seems to be processing the internal part (console.log) when the request return with the data from server side. So basically you should save the data instead of the console.log line. You could bind it somewhere or save to a variable. And it seems that player is the body variable's key you are looking for.
Maybe something like this: