I'm pretty new to Node and also to JS. I already read a lot here at stackoverflow and I think I personally have one in my brain for the last view days trying to fix this issue ;-).
The purpose of this code is to get the steam name for each player from steam API and put it into json object. after that the json object is using to present the steam data on a web page.
I got everithing working, but the first time the web page is loading it is empty. The second time which page is loading there are all data available.
The steam user ID is held in a simple array. This array is put to a forEach method and inside this method the steam API is called to return each player name. This player names are returned into the final json object, which held both, the steam id and the steam name.
input = steam ID's from array
process = get steam name from steam ID
output = json object with all steam ID's and names.
Important thing is, that the page has to wait for the API calls to finish.
I have the following code:
var cfg = require('config');
var Promise = require('bluebird');
//access steam API for steamId resolution
var Steam = require('steam-webapi');
// Set global Steam API Key
Steam.key = cfg.get('steam.api.key');
// Create a file called STEAM_KEY and stick your API key in it
// (or insert it here)
var steamAPIKey = Steam.key;
if (steamAPIKey.length === 0) {
try { steamAPIKey = fs.readFileSync('../STEAM_KEY').toString();}
catch(e) {
try { steamAPIKey = fs.readFileSync('./STEAM_KEY').toString(); }
catch(e) { console.log('No API key provided'); }
}
}
var steamconnect = function() {
return new Promise(function (resolve, reject) {
Steam.ready(steamAPIKey, function (err, data) {
if (err) return console.log(err);
else {
var steam = new Steam({key: steamAPIKey});
resolve(steam);
}
});
});
};
var getsteaminfoforeach = function(arrsid, steam){
return new Promise(function(resolve, reject){
dataarr = [];
var i = 0;
arrsid.forEach(function(entry, callback) {
if (entry !== "") {
getsteaminfo(steam, entry)
.then(function(playername) {
console.log(playername);
dataarr.push({"name": playername, "id": entry});
resolve(dataarr);
});
}
});
});
};
var getsteaminfo = function(steam, entry) {
return new Promise(function (resolve, reject) {
Promise.promisifyAll(Steam.prototype); // Creates an promise wielding function for every method (with Async attached at the end)
steam.getPlayerSummariesAsync({steamids: entry})
.then(function (data) {
playername = data.players[0]["personaname"]; //extract user name from steam user object
//dataarr.push({"name": playername, "id": entry});
console.log("get steam user: " + "name: "+playername+ " id: "+ entry);
resolve(playername);
});
});
};
var donext = function() {
return new Promise(function (resolve, reject) {
console.log("do next: "+dataarr);
});
};
arrsid = ["765611980504","765611970371",""];
steamconnect()
.then(function(steam){
console.log("arrsid: "+arrsid);
getsteaminfoforeach(arrsid, steam)
.then(function (dataarr) {
console.log("Player name: " + dataarr);
donext()
});
});
This is the output:
arrsid: 765611980504,765611970371,
get steam user: name: steamname1 id: 765611980504
steamname1
Player name: [object Object]
do next: [object Object]
get steam user: name: steamname2 id: 765611970371
steamname2
My issue is, that steamname... should be the first output (for each steamname) and after that do next: [] has to be called.
I'm sure there is a simple solution. Thank you very much!