apache / cordova-plugin-network-information

Apache Cordova Network Information Plugin
https://cordova.apache.org/
Apache License 2.0
464 stars 321 forks source link

Error: Connection is not defined #80

Closed hriziya closed 4 years ago

hriziya commented 5 years ago

I have installed the plugin correctly and it was working earlier. after few months, today I am trying to run the application on my phone and hitting with this error.

I do not see it reported by others yet so not sure if this is specific to my phone? I am on Android 8.1.0, ColorOS Version v5.1

states[Connection.UNKNOWN] = 'Unknown connection';


stack: "ReferenceError: Connection is not defined
at eval (eval at checkConnection (file:///android_asset/www/js/index.js:560:5), <anonymous>:1:1)
at checkConnection (file:///android_asset/www/js/index.js:560:5)
at doLogin 
janpio commented 5 years ago

What is your actual code? Or is it just the one line you posted?

hriziya commented 5 years ago

My code is the function to checkConnection()


function checkConnection() {
    if (navigator
       && navigator.connection
       && navigator.connection.type) {

        var networkState = navigator.connection.type;

        if(typeof(Connection) != 'undefined')
        {
            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.NONE) {
                showMessage(states[networkState], function () {
                    hideLoading();
                    return false;
                });
            }
            return networkState !== Connection.NONE;
        }
    }

    return true;
}
CenaSurya commented 5 years ago

navigate is undefined

jcesarmobile commented 5 years ago

are you waiting for device ready event before calling the plugin code?

anaaChacon commented 5 years ago

Hola, tengo un problema con las conexiones a Internet, uso el plugin Network de ionic y el problema que tengo es, cuando el tipo de conexión cambia de wifi a datos y la aplicación está en segundo plano el tipo de conexión que recibe es NONE, tengo que matar la aplicación y volver a iniciarla para que ya todo vaya correctamente, alguien ha experimentado este problema?

elbartostrikesagain commented 5 years ago

Try this if you're using phonegap:

phonegap plugin remove cordova-plugin-network-information
phonegap plugin add cordova-plugin-network-information
phonegap build android

Or this if you're using cordova without phonegap(untested):

cordova plugin remove cordova-plugin-network-information
cordova plugin add cordova-plugin-network-information
cordova build android

Longer explanation probably worth reading: For background, I generated the hello world template from phonegap and had this problem on multiple versions of phonegap, trying fresh projects and an old project. I noticed in platforms/android/platform_www/plugins there was nothing referencing cordova-plugin-network-information or even just "network". I saw other plugins listed here such as camera, battery-status, etc but nothing for network which made me think this is an issue building android and its not including the plugin at all. I deleted my android build and built it again with --verbose and noticed there was an error that happened - Failed to install 'cordova-plugin-camera': CordovaError: Uh oh!. Instead of cordova/phonegap failing, or bringing this to the front of my attention, it just continues on its way, builds the android app and says it was successful at the bottom of the output (but really it should fail if its missing a plugin).

After I manually reinstalled the plugin (and one more I needed) via the commands above, I now see platforms/android/platform_www/plugins/cordova-plugin-network-information and all is well. Not quite sure who to "blame the finger" at since this is all extremely developer unfriendly, but this is not this plugins fault for sure.

gijsbeijer commented 5 years ago

@elbartostrikesagain Worked for me! thanks!

Classic-Paterson commented 4 years ago

@elbartostrikesagain cordova plugin add cordova-plugin-network-information was the fix for me, I suspect that it was installed non globally, then another project spun up, and the dependency was lost.

breautek commented 4 years ago

Closing due to staleness.

My best advice with the information available is to ensure that you're code is only accessing these properties after the deviceready has fired.

If the issue persists, don't hesitate to open a new issue with the issue form filled out.