Unable to read undefined error type property in cordova network plugin

I am developing an Android application using cord and ion structure using the network plugin from here ( https://github.com/apache/cordova-plugin-network-information ). But the device ready alert is triggered, and then I get the following error.

"TypeError: Cannot read property 'type' of undefined 

Here is the navigator object

 navigator.connection.type 

I get an error in the next line.

+1
source share
2 answers

use mine

 document.addEventListener("deviceready", onDeviceReady, false); // Cordova is ready function onDeviceReady() { checkConnection() } function checkConnection() { 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]); } 
+3
source

For some reason, the navigator.connection object is not ready at the same time as the cordova.

You should use a timeout, like this one designed for Angular / Ionic

  setTimeout(function(){ var connection = connectionService.getConnection(); console.log("connection : "+connection); $scope.connection = connection; $scope.$apply() }, 5000) 
0
source

Source: https://habr.com/ru/post/1235680/


All Articles