无法读取属性'type'cordova网络插件中的未定义错误

Cannot read the property 'type' of undefined error in cordova network plugin

本文关键字:插件 错误 未定义 网络 type 读取 属性 cordova      更新时间:2023-09-26

我正在使用cordova和ionic框架开发android应用程序,使用来自这里(https://github.com/apache/cordova-plugin-network-information)。但设备准备就绪警报响起,然后我得到以下错误。

"TypeError: Cannot read property 'type' of undefined

这是导航器对象

navigator.connection.type 

我会在下面的行中出错。

使用我的

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]);
}

由于某些原因,navigator.connection对象没有与cordova同时准备好。

你必须使用一个超时,比如这个——为Angular/Ionic 设计的

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