简体   繁体   中英

Concatenate several API request with NodeJS

I'm messing with SteamAPI in order to learn some NodeJS. Right now I'm trying to get games's info after an initial request to get the player's profile, once I have the games IDs stored in an array. The point is that I don't know how to "return" an array AFTER the whole ID array is iterated and all results has come from the server.

function getThumbs(game) {
    return rq(
        'http://store.steampowered.com/api/appdetails?appids=' + game,
        {json: true},
        function (error, response, bd) {
            if(response.statusCode === 200 && bd[game].data) {
                return bd[game].data.screenshots;
            }
        });
}

function getGamesThumbnails(games) {
    var deferred = $q.defer(),
        queue = [];

    for (var y = 0; y < games.length; y++) {
        var game = games[y];
        var thumbs = getThumbs(game);

        queue.push(thumbs);
    }

    $q.all(queue).then(
        function (data) {
            deferred.resolve(data);
        },
        function (err) {
            deferred.reject(err)
        }
    );

    return deferred.promise;
}

    app.get('/blog',function(client_req,client_res){

    rq('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=' + key + '&steamid=blablabla&format=json', function (error, response, body) {
        var data = JSON.parse(body);
        var games = data.response.games.map(function (game) {
            return game.appid;
        });

        getGamesThumbnails(games).then(function (data) {
            console.log(data)
        })

    });
});

Your getThumbs() function does not return a promise. $q.all only works on an array containing promises, whereas rq uses callbacks.

Try this:

function getThumbs(game) {
  var deferred = $q.defer(),
  rq(
      'http://store.steampowered.com/api/appdetails?appids=' + game,
      {json: true},
      function (error, response, bd) {
          if(response.statusCode === 200 && bd[game].data) {
              deferred.resolve(bd[game].data.screenshots);
          }
      });

  return deferred.promise;
}

Basically, you should use a callback, because like you are doing in getThumbs you are returning the object, while you should return the value bd[game].data.screenshots;

function getThumbs(game, cb) {
    return rq(
        'http://store.steampowered.com/api/appdetails?appids=' + game,
        {json: true},
        function (error, response, bd) {
            if(response.statusCode === 200 && bd[game].data) {
                cb(null, bd[game].data.screenshots);
            }
        });
}

function getGamesThumbnails(games) {
    var deferred = $q.defer(),
        queue = [];

    for (var y = 0; y < games.length; y++) {
        var game = games[y];
        getThumbs(game, function(err, value) {
            queue.push(value);
        });


    }

    $q.all(queue).then(
        function (data) {
            deferred.resolve(data);
        },
        function (err) {
            deferred.reject(err)
        }
    );

    return deferred.promise;
}

And plust to return the response to the client you have to use the client_res.send(VALUE)

so the bottom part would become like this:

app.get('/blog',function(client_req,client_res){

    rq('http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=' + key + '&steamid=blablabla&format=json', function (error, response, body) {
        var data = JSON.parse(body);
        var games = data.response.games.map(function (game) {
            return game.appid;
        });

        getGamesThumbnails(games).then(function (data) {
            client_res.send(data);
            console.log(data)
        })

    });
});

Thank you both! I tried Yuri's approach, but $q.all it doesn't seem to resolve the array of promises (nothing happens after the last request from getThumbs())

  for (var y = 0; y < games.length; y++) {
    var game = games[y];
    var thumbs = getThumbs(game);

    queue.push(thumbs);
  }

$q.all(queue).then(
    function (data) {
        console.log(data)
        deferred.resolve(data);
    },
    function (err) {
        deferred.reject(err)
    }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM