Search code examples
cordovacordova-plugins

Cordova plugins "TypeError: Cannot read property 'xxxx' of undefined"


Cordova 6.0:

When window.cordova.plugins.activity.getExtras is called, I get the error

TypeError: Cannot read property 'activity' of undefined".

How to fix it? The following code is my plugin configuration:

activity.js

cordova.define("com.zlzkj.cordova.activity.activity", function(require, exports, module) {
var exec = require('cordova/exec');
function Activity() {
};
Activity.prototype={
    start : function(activityClassName,jsonData,successCallback) {
    exec(successCallback, errorCallback, "ActivityPlugin", "start", [activityClassName,jsonData]);
    },
    getExtras : function(successCallback) {
    exec(successCallback, errorCallback, "ActivityPlugin", "getExtras", []);
    }
};
var errorCallback = function(message) {
    alert("Error:" + message);
};
module.exports = new Activity();
});

config.xml

<feature name="ActivityPlugin">
        <param name="android-package" value="com.zlzkj.cordova.ActivityPlugin" />
</feature>

cordova_plugins.js

module.exports = [
    {
        "file": "plugins/cordova-plugin-whitelist/whitelist.js",
        "id": "cordova-plugin-whitelist.whitelist",
        "runs": true
    },
    {
        "file": "plugins/com.zlzkj.cordova.activity/www/activity.js",
        "id": "com.zlzkj.cordova.activity.activity",
        "clobbers": [
            "cordova.plugins.activity"
        ]
    }
];

The window.cordova.plugins.activity.getExtras and window.cordova.plugins.activity.start are called as follows:

    .controller('AppCtrl', function($scope, $ionicModal, $ionicPopover, $timeout,  $location) {
        // Form data for the login modal
        var loginData = $scope.loginData = {
            userName:"",
            privName:"",
            funcIdStr:""
        };
//'window.cordova.plugins.activity.getExtras' make error messages
    window.cordova.plugins.activity.getExtras(function(data){     
                                    loginData.userName=date.userName;
                                    loginData.privName=data.privName;
                                    loginData.funcIdStr=data.funcIdStr;
                                    $scope.$apply();
                                });

    }

    .controller('HomeCtrl', function($scope, $stateParams) {

        var activityDate = $scope.activityDate = {
            flag:false,
            name:""
        }

        $scope.goActivity = function(){
            var sendData = {
              id:10001,
              name:"Simon",
              age:28
            };
//'window.cordova.plugins.activity.start' is called correctly          
    window.cordova.plugins.activity.start("com.haiwei.ar_oaapp.activity.AdditionActivity",sendData,function(data){
                $scope.activityDate.flag=true;
                activityDate.name=data.name;
                $scope.$apply();
            });


        }

    })

Part of the code in app.js

.run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            // org.apache.cordova.statusbar required
            StatusBar.styleDefault();
        }
    });
}

Solution

  • Cordova will fire the event. You should listen to the event. No Plugin will be available before the event was fired.