Cannot read property type of undefined - PhoneGap Network Plugin



Download Brave Browser


I will tell you frankly that I have struggled so much with this issue that I had to write something about it.

Cannot read property 'type' of undefined, What does this means?


You must have gone in many forums and seen answers where some of the users suggested to add a 500ms timer to the network function right?

Well, here is the actual issue, simply paste this code(from the cordova website) to your index.html or JS file:

function checkConnection() {
    var networkState = navigator.connection.type;
    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';
    alert('Connection type: ' + states[networkState]);
}
checkConnection();

Now, call the checkConnection() function anywhere in your script.

The really really really important part....the actual bug..

Check your config.xml file and try to see if you have your plugin set this way:

    <plugin name="cordova-plugin-network-information" source="npm" spec="~2.0.1" />

You see the source="npm"  Well this is what is causing the issue. The reason all my plugins were set with the source npm was because I updated all of them via another plugin that self update everything.

Open your command prompt and type this command first:

Cordova plusing remove cordova-plugin-network-information

Now type:

Cordova plusing add cordova-plugin-network-information

Look at the confix.xml while the last command is executing, you will notice that the old plugin line will be remove and instead this will be added:


    <plugin name="cordova-plugin-network-information" spec="~2.0.1" />


Finally, plugin your Android mobile to your PC and type this command:

phonegap run android
This command will send your app in your mobile phone(the actual APK). You now be able to see the network alert.


If this helped you, please leave us a comment below, thank you!