0

我正在使用异步瀑布进行两个 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});   
  });
4

1 回答 1

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)
    });
  }
]
于 2015-05-04T23:14:39.183 回答