I'm using async waterfall to make two API calls. The second call depends on data from the first one, so it is important that the first one finishes executing before the second one starts. However, in the following code snippet, the second function always starts before the first one finishes, therefore it never gets the data. How can I please fix this? Thanks!
我正在使用异步瀑布来进行两次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});
});
});
UPDATE 1 I mistakenly placed the callback as third argument for request. I have fixed that but the code still does not behave as expected. Thanks
更新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 个解决方案
#1
I'm not sure request
takes in a 3rd argument as a function. Try calling it inside the callback, instead of as an argument like below:
我不确定请求是否将第三个参数作为函数。尝试在回调中调用它,而不是像下面的参数一样:
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)
});
}
]
#1
I'm not sure request
takes in a 3rd argument as a function. Try calling it inside the callback, instead of as an argument like below:
我不确定请求是否将第三个参数作为函数。尝试在回调中调用它,而不是像下面的参数一样:
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)
});
}
]