在异步瀑布中嵌套异步api调用

时间:2015-05-04 22:32:14

标签: node.js express request node-async

我使用异步瀑布进行两次API调用。第二个调用取决于第一个调用的数据,因此第一个调用在第二个调用开始之前完成执行非常重要。但是,在下面的代码片段中,第二个函数始终在第一个函数完成之前启动,因此它永远不会获取数据。我该怎么解决这个问题?谢谢!

 router.get("/", function(req, res, next){
  var campaign;
  var restaurant;
  async.waterfall([

    function(callback){
      var url = "http://localhost:3000/api/campaign";
      console.log("calling campaign");
      request(url, function(err, response, body) {
        // JSON body
        if(err) { console.log(err); return; }
        campaign = JSON.parse(body);

      }, callback(null, campaign));

    },
    function(obj, callback){
      console.log(obj.restaurant);
      var url = "http://localhost:3000/api/restaurantByID/"+obj.restaurant;
      console.log("calling restaurant");
      request(url, function(err, response, body) {
        // JSON body
        if(err) { console.log(err); callback(true); return; }
        restaurant = JSON.parse(body);

      }, callback(null, restaurant));


    }
  ],
  function (err, result) {
   if(err) { console.log(err); res.send(500,"Server Error"); return; }

   res.render("home.html", {"title":"Home", "campaign": campaign, "restaurant": result});   
  });


});

更新1 我错误地将回调作为请求的第三个参数。我已经修复了,但代码仍然没有按预期运行。感谢

router.get("/", function(req, res, next){
  var campaign;
  var restaurant;
  async.waterfall([

    function(callback){
      var url = "http://localhost:3000/api/campaign";
      console.log("calling campaign");
      request(url, function(err, response, body) {
        // JSON body
        if(err) { console.log(err);callback(true); return; }
        campaign = JSON.parse(body);
        callback(null, campaign.restaurant);
      });


    },
    function(restaurant, callback){
      console.log(restaurant);
      var url = "http://localhost:3000/api/restaurantByID/"+restaurant;
      console.log("calling restaurant");
      request(url, function(err, response, body) {
        // JSON body
        if(err) { console.log(err); callback(true); return; }
        restaurant = JSON.parse(body);
        callback(null, restaurant);
      });


    }
  ],
  function (err, result) {
   if(err) { console.log(err); res.send(500,"Server Error"); return; }

   res.render("home.html", {"title":"Home", "campaign": campaign, "restaurant": result});   
  });

1 个答案:

答案 0 :(得分:0)

我不确定request是否接受第三个参数作为函数。尝试在回调中调用它,而不是像下面这样的参数:

async.waterfall([

  function(callback){
    var url = "http://localhost:3000/api/campaign";
    console.log("calling campaign");
    request(url, function(err, response, body) {
      // JSON body
      if(err) { console.log(err); return; }
      campaign = JSON.parse(body);
      callback(null, restaurant)
    });
  },
  function(obj, callback){
    console.log(obj.restaurant);
    var url = "http://localhost:3000/api/restaurantByID/"+obj.restaurant;
    console.log("calling restaurant");
    request(url, function(err, response, body) {
      // JSON body
      if(err) { console.log(err); callback(true); return; }
      restaurant = JSON.parse(body);
      callback(null, restaurant)
    });
  }
]