Google SignOut 进程出错未捕获错误:nb JS
2019-06-30
898
为了重构我的代码,我编写了如下的 google SignOut 函数
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";
}
});
});
}
但它在控制台中给出了这样的错误
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)
但我像这样实现它
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";
}
});
});
});
});
};
然后它工作正常,尽管看起来不太好。如果有人能告诉我我之前的实现有什么问题以及什么不起作用。
1个回答
问题出在这行
gApiAuth.init().then(doGoogleLogout(gApiAuth));
。
对于承诺的成功处理程序,您需要传递函数引用而不是直接调用该函数。这里
doGoogleLogout(gApiAuth)
将在承诺解决之前被调用。将其更改为
gApiAuth.init().then(()=>doGoogleLogout(gApiAuth))
从此 链接
了解更多信息cauchy
2019-07-01