类型错误' undefined'不是一个对象(评估' navigator.connection.type')

时间:2015-04-06 07:24:26

标签: javascript android ios cordova

我正在建立一个Cordova应用程序,我正在使用Cordova网络信息插件org.apache.cordova.network-information。从过去的几天开始,我在网络检查器中遇到这个奇怪的错误,在safari中进行调试,其中“typeerror'undefined'不是一个对象(评估'navigator.connection.type')”。在初始加载期间它工作正常但在应用程序中进一步导航时显示此错误并且应用程序冻结。在iOS 8中,它经常发生,但在Android棒棒糖中偶尔会发生。我已经尝试了所有关于此的建议或帖子,但它导致我无处可去。我使用的是Cordova 4.3版,iOS 8.1。任何帮助非常感谢抱歉无法发布任何关于此的图片,因为我没有足够的声誉发布它

    function checkConnection() { 
    alert(navigator.connection.type);
    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';
    if(networkState == Connection.UNKNOWN || networkState == Connection.NONE){
        navigator.notification.alert('No Network Available',null,"Warning");
        sessionStorage.setItem('UserID',"");
        $.mobile.changePage("#loginPage", {
                            transition: 'none',
                            showLoadMsg: true
                            });
        return false;
    }else{
        return true;
   }
}

3 个答案:

答案 0 :(得分:1)

检查您是否已声明这些

<feature name="NetworkStatus">
         <param name="android-package" value="CDVConnection" />
 </feature>

网络状态将是功能名称,值将是您的班级名称。

在Android Manifest中,

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Aso check cordova_plugin.js

cordova.define('cordova/plugin_list', function(require, exports, module) {
    module.exports = [{
        "file": "plugins/org.apache.cordova.dialogs/www/notification.js",
        "id": "org.apache.cordova.dialogs.notification",
        "merges": ["navigator.notification"]
    }, {
        "file": "plugins/org.apache.cordova.network-information/www/network.js",
        "id": "org.apache.cordova.network-information.network",
        "clobbers": ["navigator.connection", "navigator.network.connection"]
    }];
    module.exports.metadata = // TOP OF METADATA 
    {
        "org.apache.cordova.device": "0.2.8",
        "org.apache.cordova.network-information": "0.2.7"
    }
});

答案 1 :(得分:1)

在我的情况下,问题是我从device ready调用了该函数 所以这样做解决了我的问题:

document.addEventListener("deviceready", function(){
     // do your  navigator.connection.type stuff here 
    }, false);

答案 2 :(得分:0)

我使用cordova-plugin-network-information时遇到了同样的问题。我注意到的是,尽管在“deviceready”被触发后被调用,但navigator.connection.type对象仍未定义。我的解决方法是将呼叫延迟200ms,之后该对象可供使用。

function checkConnection(){
    try{
        console.log(navigator.connection.type);
    }
    catch(e){
        alert("error : "+e);
    }

    setTimeout(function(){
        var networkState = navigator.connection.type;
        console.log(networkState);

        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]);
        if(states[networkState] == "No network connection"){
            window.location.assign("lostConnection.html");
        }
    }, 200);
}

所以“try / catch”序列会因未定义的对象而失败,但200ms之后对navigator.connection.type的调用是成功的,无论如何对我来说都是好的!