`Uncaught TypeError: Cannot read property 'signOut' of undefined`
`Uncaught TypeError: Cannot read property 'signOut' of undefined`
我收到错误:
Uncaught TypeError: Cannot read property 'signOut' of undefined
这一行:auth2.signOut()
我在下面的页面上没有 google 登录按钮。
我也尝试执行函数 signOutGoogle
,但这也会导致错误。
页首 我的页面在 <head>
:
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com">
在 </body>
标签之前我有:
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
JavaScript代码:
function onLoad() {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
});
auth2 = gapi.auth2.getAuthInstance();
});
//check if url contains ?signout=true
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
}
更新 1
由于某种原因 ga
被调用(我自己并没有明确地这样做)并且失败了,这里发生了什么?
我已经检查过这里:
- HTML PHP google single sign on signout will throw "Cannot read property 'getAuthInstance' of undefined"
更新 2
现在我遇到错误
Uncaught Error: ia
更新 3
我尝试从脚本调用中删除调用 noload 然后将其添加到 document.ready
以调用 signout
函数,但是即使使用@Vishal 的代码 API 还没有可用:
<script src="https://apis.google.com/js/platform.js"></script></body>
并且:
$(document).ready(function () {
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
}).then(() => {
auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.error('User signed out');
});
}).catch(err => {
console.log(err);
});
});
}
});
您的问题很可能是您的代码在定义 auth2
变量之前调用了 signOut
函数。根据您的代码,来自 gapi.load
的回调不会立即执行,并且因为 JavaScript 已编译,所以
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
正在调用您的部分代码,而 auth2
仍然是 undefined
。尝试将其放入回调中,如下所示:
function onLoad() {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
});
auth2 = gapi.auth2.getAuthInstance();
//check if url contains ?signout=true
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
});
}
function onLoad() {
console.error("onLoad executed.");
var auth2;
const oauthLogin = new Promise((resolve, reject) => {
gapi.load("auth2", function () {
auth2 = gapi.auth2.init({
client_id: "MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com",
});
auth2 = gapi.auth2.getAuthInstance();
});
});
oauthLogin.then(() => {
if (window.location.href.indexOf("?signout=true") != -1) {
console.error("param found");
auth2.signOut().then(function () {
console.error("User signed out");
});
}
});
}
您可以将一个承诺分配给一个变量,并在它像这样解析后执行一些代码。
有多种方法可以实现这一点,请查看 Promise.prototype.then() 参考资料。
顺便说一下,您不需要将 window.location.href 解析为字符串,因为它 location.href
是一个字符串化器 returns 一个适合您目的的 USVString。
您的 javascript 代码应该如下所示,在代码中,我在 init 操作的 then() 部分添加了注销执行。因此注销代码仅在 init 返回服务器响应后执行。
function onLoad() {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
}).then(() => {
auth2 = gapi.auth2.getAuthInstance();
//check if url contains ?signout=true
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
}).catch(err => {
console.log(err);
});
});
}
我收到错误:
Uncaught TypeError: Cannot read property 'signOut' of undefined
这一行:auth2.signOut()
我在下面的页面上没有 google 登录按钮。
我也尝试执行函数 signOutGoogle
,但这也会导致错误。
页首 我的页面在 <head>
:
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com">
在 </body>
标签之前我有:
<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
JavaScript代码:
function onLoad() {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
});
auth2 = gapi.auth2.getAuthInstance();
});
//check if url contains ?signout=true
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
}
更新 1
由于某种原因 ga
被调用(我自己并没有明确地这样做)并且失败了,这里发生了什么?
我已经检查过这里:
- HTML PHP google single sign on signout will throw "Cannot read property 'getAuthInstance' of undefined"
更新 2
现在我遇到错误
Uncaught Error: ia
更新 3
我尝试从脚本调用中删除调用 noload 然后将其添加到 document.ready
以调用 signout
函数,但是即使使用@Vishal 的代码 API 还没有可用:
<script src="https://apis.google.com/js/platform.js"></script></body>
并且:
$(document).ready(function () {
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
}).then(() => {
auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.error('User signed out');
});
}).catch(err => {
console.log(err);
});
});
}
});
您的问题很可能是您的代码在定义 auth2
变量之前调用了 signOut
函数。根据您的代码,来自 gapi.load
的回调不会立即执行,并且因为 JavaScript 已编译,所以
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
正在调用您的部分代码,而 auth2
仍然是 undefined
。尝试将其放入回调中,如下所示:
function onLoad() {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
});
auth2 = gapi.auth2.getAuthInstance();
//check if url contains ?signout=true
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
});
}
function onLoad() {
console.error("onLoad executed.");
var auth2;
const oauthLogin = new Promise((resolve, reject) => {
gapi.load("auth2", function () {
auth2 = gapi.auth2.init({
client_id: "MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com",
});
auth2 = gapi.auth2.getAuthInstance();
});
});
oauthLogin.then(() => {
if (window.location.href.indexOf("?signout=true") != -1) {
console.error("param found");
auth2.signOut().then(function () {
console.error("User signed out");
});
}
});
}
您可以将一个承诺分配给一个变量,并在它像这样解析后执行一些代码。 有多种方法可以实现这一点,请查看 Promise.prototype.then() 参考资料。
顺便说一下,您不需要将 window.location.href 解析为字符串,因为它 location.href
是一个字符串化器 returns 一个适合您目的的 USVString。
您的 javascript 代码应该如下所示,在代码中,我在 init 操作的 then() 部分添加了注销执行。因此注销代码仅在 init 返回服务器响应后执行。
function onLoad() {
console.error('onLoad executed.');
var auth2;
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: 'MYAPPIDPLACEHOLDERWHICHIFILLEDOUT.apps.googleusercontent.com'
}).then(() => {
auth2 = gapi.auth2.getAuthInstance();
//check if url contains ?signout=true
var url = window.location.href;
if (url.toString().indexOf('?signout=true') != -1) {
console.error('param found');
auth2.signOut().then(function () {
console.error('User signed out');
});
}
}).catch(err => {
console.log(err);
});
});
}