未捕获的类型错误:无法读取 null 的属性“signIn”(在 localhost 上运行代码时!)
Posted
技术标签:
【中文标题】未捕获的类型错误:无法读取 null 的属性“signIn”(在 localhost 上运行代码时!)【英文标题】:Uncaught TypeError: Cannot read property 'signIn' of null (When running the code on localhost!) 【发布时间】:2021-05-15 23:31:33 【问题描述】:我在“js.do”上运行了这段代码,它没有任何问题。但是,当我在 localhost 上运行它时,出现以下错误:“Uncaught TypeError: Cannot read property 'signIn' of null”。
<script src="https://apis.google.com/js/api.js"></script>
<script>
var CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
/**
* Sample javascript code for reseller.subscriptions.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function authenticate()
return gapi.auth2.getAuthInstance()
.signIn(scope: "https://www.googleapis.com/auth/apps.order https://www.googleapis.com/auth/apps.order.readonly")
.then(function() console.log("Sign-in successful"); ,
function(err) console.error("Error signing in", err); );
function loadClient() code to load client
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() // code to execute API read commands
function checkStatus (response,counter)
console.log("inside checkStatus",response.seats);
return response.seats.numberOfSeats > 0;
gapi.load("client:auth2", function()
gapi.auth2.init(
client_id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
);
);
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
这可能是什么原因?
【问题讨论】:
【参考方案1】:gapi.load("client:auth2", function() ...)
是导致问题的原因。我收到此错误“gapi.auth2.ExternallyVisibleError: Invalid cookiePolicy”,因为我试图通过直接在本地访问我的文件 (index.html) 来测试 API。 Google 登录 API 仅适用于正在运行的网络服务器,如以下帖子 (click here) 中所回答的那样
【讨论】:
【参考方案2】:我假设它是身份验证功能中的登录。您可以尝试在返回之前执行console.log(gapi.auth2.getAuthInstance())
并查看返回的内容。可能会给你一个线索。
【讨论】:
它给出的是“null” 实际上这帮助我解决了空原因。 gapi.load("client:auth2", function() ... 是导致问题的原因。以上是关于未捕获的类型错误:无法读取 null 的属性“signIn”(在 localhost 上运行代码时!)的主要内容,如果未能解决你的问题,请参考以下文章
未捕获的类型错误:无法读取 null 的属性“getContext”
为啥我会收到此错误:未捕获的类型错误:无法读取 null 的属性 'classList'
未捕获的类型错误:在输入单击时无法读取 null 的属性“样式”