"Uncaught TypeError: Cannot read property 'on' of undefined"


#1

I have a fairly generic app that I am trying to test ionic push with but when I run in the code on an android emulator I am getting an error as shown in the title regarding on of undefined. The on is a reference to an EventEmitter attached to the this value so it could very well be scoping issue. I followed the instructions on ionic however this is the error I keep getting. When I ran this in test mode where dev_push true, it worked while doing ionic serve. However it does not work on my current setup even though I followed the directions to a tee. If anyone has any input I will gladly provide more information but hopefully I just missed something simple.

Plugins:

ionic plugin list
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-geolocation 2.1.0 "Geolocation"
cordova-plugin-whitelist 1.2.1 "Whitelist"
phonegap-plugin-push 1.6.1 “PushPlugin”

CordovaPlugins in package.json:

“cordovaPlugins”: [
“cordova-plugin-device”,
“cordova-plugin-console”,
“cordova-plugin-whitelist”,
“cordova-plugin-splashscreen”,
“cordova-plugin-statusbar”,
“ionic-plugin-keyboard”,
“cordova-plugin-geolocation”,
{
“variables”: {
“SENDER_ID”: “MY_SENDER_ID”
},
“locator”: “phonegap-plugin-push”,
“id”: “phonegap-plugin-push”
}
],

error:

04-03 06:12:02.880 3248-3248/com.ionicframework.MYAPPI/chromium: [INFO:CONSOLE(26)] “Starting ionicPush”, source: file:///android_asset/www/js/app.js (26)
04-03 06:12:02.880 3248-3248/com.ionicframework.MYAPPI/chromium: [INFO:CONSOLE(2)] “Ionic Push:”, source: file:///android_asset/www/lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js (2)
04-03 06:12:02.880 3248-3248/com.ionicframework.MYAPPI/chromium: [INFO:CONSOLE(2)] “Ionic Push:”, source: file:///android_asset/www/lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js (2)
04-03 06:12:02.880 3248-3248/com.ionicframework.MYAPPI/chromium: [INFO:CONSOLE(3)] “Uncaught TypeError: Cannot read property ‘on’ of undefined”, source: file:///android_asset/www/lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js (3)

That line of code (from the ionic-platform-web-client):

/**
* Fire a callback when Push is ready. This will fire immediately if
* the service has already initialized.
*
* @param {function} callback Callback function to fire off
* @return {void}
*/
}, {
key: “onReady”,
value: function onReady(callback) {
var self = this;
if (this._isReady) {
callback(self);
} else {
self._emitter.on(‘ionic_push:ready’, function () {
callback(self);
});
}
}
}

My code:

$ionicPlatform.ready(function() {

console.log('Starting ionicPush');
push = new Ionic.Push();
push.register(function(data) {
   token = data.token;
   console.log("Got Token:", data.token);
});
...

My index.html

<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="js/app.js"></script>

#2

hi,

please read this in the docs


#3

I appreciate the quick response however as mentioned in the OP, this is not an issue related to ionic serve. This is actually on an android vd.

Maybe I should have specified, it worked on ionic serve but when changing it over to ‘full’ where dev_push = false and testing it on an avd it fails because of a different issue.


#4

Hi derekrada, still having the same problem? if not what was your solution?

After i set the index.html to take the non-minified version, it was clear the self._emitter remains undefined.
Only after I tried using Ionic 2.0 (in 1.0 i didnt have a problem at all) i first got the issue. Even after I created a blank project, added necessary plugins etc and working fine in development mode (dev true), I got this error after switching to dev false.

Although I updated with “ionic lib update”, and i got the issue before I did the update on the blank project, could it be that i got corrupted lib files somewhere? Or some environmental thing?

Anyone please?


#5

If I’m not missing something, in the FAQ is written that this works only on a physical device:

This error is often the result of testing push registration using ionic serve or a device emulator. In these cases, Cordova will not initialize the push notification service. For this reason, push notifications must be tested on a physical device.


#6

Just have this error too even on a device.

Did ionic io init. Removed/Added platform. Still not working.