简体   繁体   中英

Uncaught TypeError: Cannot read property 'isAvailable' of undefined;

I am trying to get the tutorial from Cordova GooglePlus to work. I havent spent about 16 hours on it over the past 3 days, combing stackoverflow, google, and github and I've come far but I keep getting an uncaught type error. It seems that I am not referencing the functions in GooglePlus.js correctly. Any tips would be appreciated. Here's the apk I created, which gives the same errors 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>

At a glance, it looks like you may be missing an identifier in the chain.

Your isAvailable() function:

window.plugins.isAvailable(...);

vs repo's:

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

The scripts in your code have relative paths so I can't test it, but googleplus is present elsewhere in your code, specifically as a property of window.plugins , so I'm guessing that's the culplrit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM