无法读取cordova网络插件中未定义错误的属性“类型”

时间:2014-10-20 04:55:14

标签: javascript android cordova cordova-plugins

我正在使用cordova和离子框架开发android应用程序,使用来自的网络插件 在这里(https://github.com/apache/cordova-plugin-network-information)。但设备就绪警报会触发,然后我收到以下错误。

"TypeError: Cannot read property 'type' of undefined

这是导航器对象

navigator.connection.type 

我会在下一行中收到错误。

2 个答案:

答案 0 :(得分:4)

使用我的

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

答案 1 :(得分:0)

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

你必须使用一个超时,例如这个 - 为Angular / Ionic设计

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