0

Hello guys I'm trying to use the values received from the first api url request as params of the second api url. Therefore, I've created a for loop and with the idea that for every element in the array I'm iterating will send it as the parameter (these are as you can see below the variables user, language, city).

But I think it's not working because for loop is synchronous and the module request from nodejs is asynchronous. However I can't figure out another way to do it...

request(firstUrl, function(err, res, body) {

    let input = JSON.parse(body);

       for( var i=0 ;i< input.length; i++){

		    var user= input[i]["user"]
            var language= input[i]["language"]
            var city= input[i]["city"]
		    var query_url= encodeURIComponent('{"data":{"call":{"data":[{"text":'+city+',"language":'+language+',"user":'+user+',"id":'+id+'}]}}}')

              var secondUrl = {  

                url: `http://api.com/?request=${query_url}`,
                method: 'GET',
                headers: {
                  "Content-type": "application/json"

                }
              };                            
             request(secondUrl, function(err, res, body) {  
               let input = JSON.parse(body);
               console.log(err)
               console.log(res)
               console.log(input)

             }); 
          }

		return mongo.multiSave(input.contents)
                  
});

4

2 回答 2

1

事实上,正如前面的答案,promise 可以通过更具可读性的代码轻松实现这一点。结帐请求-承诺。我还使用Promise.all并行解决所有第二个请求承诺。这是我的解决方案(我没有测试过代码)

request(firstUrl).then(function(body) {
  return JSON.parse(body);
}).then(function(input) {
  return Promise.all(secondRequests(input));
}).then(function(bodies) {
  bodies.forEach(function(body) {
    let input = JSON.parse(body);
    console.log(err)
    console.log(res)
    console.log(input)
  })
}).catch(function (err) {
});

function secondRequests(input) {
  var promises = [];
  for( var i=0 ;i< input.length; i++){
    var user= input[i]["user"]
    var language= input[i]["language"]
    var city= input[i]["city"]
    var query_url= encodeURIComponent('{"data":{"call":{"data":[{"text":'+city+',"language":'+language+',"user":'+user+',"id":'+id+'}]}}}')
    var secondUrl = {
      url: `http://api.com/?request=${query_url}`,
      method: 'GET',
      headers: {
        "Content-type": "application/json"
      }
    };
    promises.push(request(secondUrl));
  }
  return promises;
}
于 2017-04-21T17:57:11.923 回答
0

尝试使用request-promise。这将帮助您解决

                var options = {
                method: 'POST',
                uri: 'http://api.posttestserver.com/post',
                body: {
                    some: 'payload'
                },
                json: true // Automatically stringifies the body to JSON
            };
            request(options).then(function (parsedBody) {

                let input = JSON.parse(parsedBody);

                for (var i = 0; i < input.length; i++) {

                    var user = input[i]["user"]
                    var language = input[i]["language"]
                    var city = input[i]["city"]
                    var query_url = encodeURIComponent('{"data":{"call":{"data":[{"text":' + city + ',"language":' + language + ',"user":' + user + ',"id":' + id + '}]}}}')

                    var secondOptions = {

                        url: `http://api.com/?request=${query_url}`,
                        method: 'GET',
                        headers: {
                            "Content-type": "application/json"

                        }
                    };

                    request(secondOptions).then(function (body) {
                        let input = JSON.parse(body);
                        console.log(err)
                        console.log(res)
                        console.log(input)

                    })

                }

                return mongo.multiSave(input.contents)


            }).catch(function (err) {

            });
于 2017-04-21T17:00:46.313 回答