FB Graph Api FB.getLoginStatus包装函数返回undefined

时间:2014-02-15 20:17:17

标签: facebook facebook-graph-api

我为FB.getLoginStatus编写了一个包装函数,定义如下:

this.isLoggedInFB = function() {
    FB.getLoginStatus(function(response) {
        if(response.status == 'connected')
        {
            return true;
        }
        else if(response.status =='not_authorized'){
            return true;
        else{
            return false;
        }
    });
}

每当我发出以下警告

alert(myClass.isLoggedInFB());

我得到undefined

当我在isLoggedInFB正文中添加提醒以查看是否已定义FB时,我得到:

[Object object]

告诉我它已被调用FB.init,因此令人困惑 这是我认为唯一可能出错的事情。

最后,我做了一个警告,看看我输错了名称或者我的姓名间距是错误的

alert(myClass.isLoggedInFB);

我得到了正确的函数定义。

1 个答案:

答案 0 :(得分:1)

因为函数isLoggedInFB不返回任何值。如果您需要获取响应数据,则需要返回如下值:

var connected = 0;
this.isLoggedInFB = function() {
    FB.getLoginStatus(function(response) {
        if(response.status == 'connected')
        {
            connected = 1;
            return true;
        }
        else if(response.status =='not_authorized'){
        {
            connected = 1;
            return true;
        }
        else{
            connected = 0;
            return false;
        }
    });
    return connected;
}