简体   繁体   English

将多个API请求与NodeJS连接

[英]Concatenate several API request with NodeJS

I'm messing with SteamAPI in order to learn some NodeJS. 我搞砸了SteamAPI以便学习一些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. 现在,一旦我将游戏ID存储在数组中,我就试图在最初请求获取玩家资料后获取游戏信息。 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. 关键是在迭代整个ID数组并且所有结果都来自服务器之后,我不知道如何“返回”数组。

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. 您的getThumbs()函数不会返回promise。 $q.all only works on an array containing promises, whereas rq uses callbacks. $ q.all仅适用于包含promise的数组,而rq使用回调。

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; 基本上,您应该使用回调,因为就像在getThumbs中所做的getThumbs您正在返回对象,而您应该返回值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) 为了将响应返回给客户端,您必须使用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()) 我尝试了尤里(Yuri)的方法,但是$ q.all似乎都无法解决承诺数组(在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)
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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