【问题标题】:Uncaught TypeError: Cannot read property 'isAvailable' of undefined;未捕获的类型错误:无法读取未定义的属性“isAvailable”;
【发布时间】:2018-08-31 17:47:10
【问题描述】:

我正在尝试从Cordova GooglePlus 获取教程。在过去的 3 天里,我没有在它上面花费大约 16 个小时,结合了 stackoverflow、google 和 github,我已经走了很远,但我一直收到一个未捕获的类型错误。看来我没有正确引用 GooglePlus.js 中的函数。任何提示将不胜感激。这是我创建的 apk,它给出了相同的错误App.apk

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height"/>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
    <meta name="msapplication-tap-highlight" content="no"/>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript" src="plugins/cordova-plugin-googleplus/www/GooglePlus.js"></script>
    <title>Hello World</title>
    <script type="text/javascript">
      app.initialize();
      function onLoad() {
            document.addEventListener("deviceready", onDeviceReady, false);
        }
      function isAvailable() {
        window.plugins.googleplus.isAvailable(function(avail) {alert(avail)});
      }
      function login() {
        window.plugins.googleplus.login(
            {},
            function (obj) {
              document.querySelector("#image").src = obj.imageUrl;
              document.querySelector("#image").style.visibility = 'visible';
              document.querySelector("#feedback").innerHTML = "Hi, " + obj.displayName + ", " + obj.email;
            },
            function (msg) {
              document.querySelector("#feedback").innerHTML = "error: " + msg;
            }
        );
      }
      function trySilentLogin() {
        window.plugins.googleplus.trySilentLogin(
            {},
            function (obj) {
              document.querySelector("#image").src = obj.imageUrl;
              document.querySelector("#image").style.visibility = 'visible';
              document.querySelector("#feedback").innerHTML = "Silent hi, " + obj.displayName + ", " + obj.email;
            },
            function (msg) {
              document.querySelector("#feedback").innerHTML = "error: " + msg;
            }
        );
      }
      function logout() {
        window.plugins.googleplus.logout(
            function (msg) {
              document.querySelector("#image").style.visibility = 'hidden';
              document.querySelector("#feedback").innerHTML = msg;
            },
            function (msg) {
              document.querySelector("#feedback").innerHTML = msg;
            }
        );
      }
      function disconnect() {
        window.plugins.googleplus.disconnect(
            function (msg) {
              document.querySelector("#image").style.visibility = 'hidden';
              document.querySelector("#feedback").innerHTML = msg;
            },
            function (msg) {
              document.querySelector("#feedback").innerHTML = msg;
            }
        );
      }
      window.onerror = function(what, line, file) {
        alert(what + '; ' + line + '; ' + file);
      };
      function handleOpenURL (url) {
        document.querySelector("#feedback").innerHTML = "App was opened by URL: " + url;
      }
</script>
</head>
<body onload="onLoad">
<div class="app">
    <img id="image" style="position:absolute; top:10px; left:10px" src="" />

    <h1>Google+</h1>

    <div id="deviceready" class="blink">
        <p class="event listening">Connecting to Device</p>
        <p class="event received">Device is Ready</p>

        <p id="feedback">not logged in</p>
        <button onclick="isAvailable()">Available?</button>
        <br/><br/>
        <button onclick="login()">Login with Google+</button>
        <br/><br/>
        <button onclick="trySilentLogin()">Try silent login with Google+</button>
        <br/><br/>
        <button onclick="logout()">Logout</button>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <button onclick="disconnect()">Disconnect</button>
        <br/><br/>
        <button onclick="window.plugins.googleplus.getSigningCertificateFingerprint(function(res){alert(res)}, function(res){alert(res)})">get cert fingerprint (Android)</button>
    </div>
</div>
</body>
</html>

【问题讨论】:

    标签: javascript android cordova-plugins phonegap


    【解决方案1】:

    乍一看,您可能在链中缺少一个标识符。

    你的 isAvailable() 函数:

    window.plugins.isAvailable(...);
    

    vs 回购:

    window.plugins.googleplus.isAvailable(...);
    

    您代码中的脚本具有相对路径,因此我无法对其进行测试,但googleplus 存在于您代码中的其他地方,特别是作为window.plugins 的属性,所以我猜这是罪魁祸首。

    【讨论】:

    • 啊是的,这是一个错字。我正在玩直接拉 GooglePlus.js 并尝试以这种方式调用该函数,而不是通过 window.plugins.googleplus。不过它对我不起作用,所以我在发布之前尝试将其转换回原始演示,但忘记了那部分。我现在在这里和本地编辑它
    猜你喜欢
    • 2021-08-07
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 2019-02-26
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多