返回值为空,之后填充

时间:2018-05-15 11:28:32

标签: javascript facebook object undefined return-value

我有一些基本的facebook api调用函数,我想在facebook中调用/ me并返回此对象。但每次我尝试这个,对象都是空的,之后它就会填满。我想直接填写它。

//constructor of User
function User(id, name){
    this.id = id;
    this.name = name;
}

//FB-develop configuration
window.fbAsyncInit = function() {
    FB.init({
        appId: '{my-key}',
        cookie: true,
        xfbml: true,
        version: 'v2.8'
    });

    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });
};

function testAPI() {
    var user = new User();
    FB.api('/me', function(response) {
        user.id = response.id;
        user.name = response.name;
        console.log(response);
    });
    return user;
}

function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    if (response.status === 'connected') {
        $(function() {
            var user = testAPI();
            console.log(user);
        });
    } else {
        document.getElementById('status').innerHTML = 'Please log ' +
            'into this app.';
    }
}

在statusChangeCallback(响应)中,当它在if语句中时,它控制台.log:

User {id: undefined, name: undefined}
    id : "10215297466340337"
    name : "BozZ lastname"

我不明白为什么它未定义。

0 个答案:

没有答案