[英]Javascript SDK: can i have another FB.api() calls inside FB.api()
// count total no. of groups created by me
function totalGroups(response) {
FB.api('/me/groups', {fields:'owner'}, function(g_response) {
for (i in g_response.data) {
FB.api('/me', function(m_response) {
var c = 0;
if (g_response.data[i].owner.name == m_response.name) {
c++;
}
});
}
console.log('Total:' +c);
});
}
hi, can i have another FB.api() calls inside FB.api() like i do on the above code because i can't get the value for if (g_response.data[i].owner.name == m_response.name) 嗨,我可以像在上面的代码中一样在FB.api()内进行另一个FB.api()调用,因为我无法获取if(g_response.data [i] .owner.name == m_response的值。名称)
Yes, you can embed a FB.api() call inside another Fb.api() call but the flow is guaranteed as the FB.api() calls are asynchronous. 是的,您可以将FB.api()调用嵌入到另一个Fb.api()调用中,但是由于FB.api()调用是异步的,因此可以保证流量。 One issue I found with your code is related to the
var c
. 我发现与您的代码有关的一个问题与
var c
。 First of all it's out of scope for console.log('Total:' +c)
method and moreover you have declared it inside the loop which means it's value will be reset after each loop execution. 首先,它超出
console.log('Total:' +c)
方法的范围,而且您已在循环内声明了它,这意味着它将在每次循环执行后重置其值。
Try this: 尝试这个:
// count total no. of groups created by me
function totalGroups(response) {
var c = 0;
FB.api('/me/groups', {fields:'owner'}, function(g_response) {
for (i in g_response.data) {
FB.api('/me', function(m_response) {
if (i.owner.name == m_response.name) {
c++;
}
});
}
console.log('Total:' +c);
});
}
this is how i fix the problem, i know its ugly but it's the best i can do at the moment. 这就是我解决问题的方式,我知道它很丑,但这是我目前能做的最好的事情。
function totalGroups(response) {
var c = 0;
FB.api('/me', function(m_response) {
FB.api('/me/groups', {fields:'owner'}, function(g_response) {
for (i in g_response.data) {
if (g_response.data[i].owner) {
if (g_response.data[i].owner.name == m_response.name) {
c++;
}
}
}
console.log('Total: ' + c);
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.