Error in Google SignOut process Uncaught Error: nb JS

Viewed 827

Just to make my code refactored I have written google SignOut function as follows

function googleSignOut() {
    if (typeof module_google_login == 'undefined') {
        return false;
    }
    gapi.load('auth2', function () {
        var gApiAuth = gapi.auth2;
        gApiAuth.init().then(doGoogleLogout(gApiAuth));
    });
};

function doGoogleLogout(gApiAuth) {
    console.log(gApiAuth);
    var googleAuth = gApiAuth.getAuthInstance();
    googleAuth.signOut().then(function () {
        $.ajax({
            type: 'POST',
            url: '/account/logout/',
            success: function () {
                auth2.disconnect();
                window.location = "/account";
            }
        });
    });
}

But it gives an error in the console like this

Uncaught Error: nb
    at tE (cb=gapi.loaded_0:201)
    at jF.<anonymous> (cb=gapi.loaded_0:248)
    at new _.C (cb=gapi.loaded_0:123)
    at jF.BT (cb=gapi.loaded_0:248)
    at Ay.Qv.a.<computed> [as signOut] (cb=gapi.loaded_0:227)
    at doGoogleLogout (main.js:48)
    at main.js:41
    at platform.js:18
    at Sa (platform.js:10)
    at Y (platform.js:18)

But I implement it like this

function googleSignOut() {
    if (typeof module_google_login == 'undefined') {
        return false;
    }
    gapi.load('auth2', function () {
        gapi.auth2.init().then(function () {
            var auth2 = gapi.auth2.getAuthInstance();
            auth2.signOut().then(function () {
                $.ajax({
                    type: 'POST',
                    url: '/account/logout/',
                    success: function () {

                        window.location = "/account";
                    }
                });
            });
        });
    });
};

then it works fine though it does not look that good. If someone can tell me what is wrong with my previous implementation and what that is not working.

1 Answers

Problem is with this line gApiAuth.init().then(doGoogleLogout(gApiAuth));.

For success handler of the promise, you need to pass functional reference not call the function directly. Here doGoogleLogout(gApiAuth) will be called before the promise is resolved. Change this to gApiAuth.init().then(()=>doGoogleLogout(gApiAuth))

Read more about from this link

Related