2

I got some basic facebook api call functions, I would like to make a call to /me in facebook and returning this object. But every time I try this, the objects is empty and afterwards it fills. I would like to make it directly filled.

//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.';
    }
}

In statusChangeCallback(response) when it's in the if statement, it console.log this:

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

I don't understand why it's undefined.

BozZ
  • 83
  • 1
  • 7

0 Answers0