1
// 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)

4

2 回答 2

0

这就是我解决问题的方法,我知道它很丑,但这是我目前能做的最好的。

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);
        });
    });
}
于 2013-04-24T12:02:43.493 回答
0

是的,您可以在另一个 Fb.api() 调用中嵌入一个 FB.api() 调用,但由于 FB.api() 调用是异步的,因此可以保证流程。我在您的代码中发现的一个问题与var c. 首先,它超出了console.log('Total:' +c)方法的范围,而且您已经在循环中声明了它,这意味着它的值将在每次循环执行后重置。

尝试这个:

// 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);
});
}
于 2013-04-22T08:09:29.647 回答