Node.js/express,javascript回调函数未执行
Node.js / express, javascript callback function not getting executed
/* GET home page. */
router.get('/home/', function(req, res, next) {
var code = req.query.code;
req.SC.authorize(code, function(err, accessToken) {
if ( err ) {
throw err;
} else {
req.session.oauth_token = accessToken;
// Client is now authorized and able to make API calls
//res.render('home', { token: accessToken });
var url = 'https://api.soundcloud.com/me?oauth_token=' + accessToken;
requestify.get(url).then(function(response){
var user = response.getBody();
req.session.user = user;
var user_url = config.base_url + '/api/users/add';
var options = { user: user };
requestify.post(user_url, options).then(function(response){
console.log("done with users/add")
var href = 'https://api.soundcloud.com/users/' + user.id
+ '/favorites?client_id=' + config.auth.client_id + '&linked_partitioning=1&limit=200';
soundcloud.getCollection(req, res, [], href, function(collection){
console.log("can't get here...");
//console.log(collection);
res.json(collection);
//return collection;
});
/*
var collection_url = config.base_url + '/api/collections/add';
requestify.post(collection_url, options).then(function(response){
console.log("done with collections/add")
res.json(response);
})
*/
});
});
}
});
});
function getCollection(req, res, collection, next_href, done){
console.log("here");
requestify.get(next_href).then(function(response){
var updatedCollection = collection.concat(response.getBody().collection);
if (next_href && updatedCollection.length < 500){
var href = response.getBody().next_href;
getCollection(req, res, updatedCollection, href);
}
else {
console.log("done");
done(updatedCollection);
}
//res.json(response.getBody());
});
}
我看到的行为是,集合已经正确构建,console.log("done")显示在控制台中,但在我调用done(updatedCollection)后,我传入的回调函数不会执行。没有print语句,没有json呈现。你们明白问题出在哪里了吗?
您递归地调用getCollection
函数而不进行回调,因此下次调用它时,done
是未定义的。
将回调传递给递归调用以及
function getCollection(req, res, collection, next_href, done) {
requestify.get(next_href).then(function(response){
var updatedCollection = collection.concat(response.getBody().collection);
if (next_href && updatedCollection.length < 500){
var href = response.getBody().next_href;
getCollection(req, res, updatedCollection, href, done); // <- HERE
} else {
console.log("done");
done(updatedCollection);
}
//res.json(response.getBody());
});
}
相关文章:
- JavaScript回调函数
- 如何在回调函数中执行流
- 回调函数中传递参数的困难(Google Map API Markers)
- 如何正确地将参数传递给RequireJS回调函数
- 回调函数在python代码中离线
- 从AJAX回调函数中分离数据
- 赋值后的回调函数
- 如何在javascript回调函数中返回多个变量
- 函数表达式,返回回调函数
- 等待回调函数执行
- JavaScript回调函数和Google Feed API
- 如何从ajax成功回调函数中读取javascript变量
- 正在保存JavaScript内部回调函数中的值
- 有没有什么方法可以停止Jquery中的animate(也可以停止完整的回调函数)
- 如何在javascript中添加带有回调函数的按钮点击事件作为window.conf
- 在Golang回调函数中启用CORS
- 在初始函数完成之前调用回调函数
- jQuery Mobile:如何在$.Mobile.changePage之前运行回调函数
- 即使使用回调函数也无法返回值
- 为什么prototypjs观察到回调函数有绑定