javascriptnetwork-programmingionic-frameworkcordova-pluginsngcordova

ngCordova: Uncaught ReferenceError: Connection is not defined


Trying to figure out this error in my Ionic app for checking network connection. Im using ngCordova's network tools found here :

http://ngcordova.com/docs/plugins/network/

ngCordova claims that their $cordovaNetwork.isOnline() function works but I'm finding quite the opposite. $cordovaNetwork.getNetwork seems to work fine but otherwise, I am getting this error when executing console.log($cordovaNetwork.inOnline()); in the code.

error

I've seen answers to this issue elsewhere but none of them involve using this function. They involve using a states array or 'online/offline' events.

Can someone explain why isOnline() || isOffline() doesn't seem to work? How can I use this function accordingly without any circus tricks? I am debugging via Android. I am injecting properly and doing other things properly in the code. Any help is appreciated. Thanks.


Solution

  • To Solve this, I had 2 workarounds for this problem 1- In Cordova/service.

    this.isOffline = function() {
        if (navigator.connection && typeof Connection !== 'undefined') {
            me.offline = $cordovaNetwork.isOffline();
            return me.offline;
        }
        return me.offline;
     };
    

    2- Include a new file connection.js in index.html(first js file) with below content

    var Connection = {
    UNKNOWN: "unknown",
    ETHERNET: "ethernet",
    WIFI: "wifi",
    CELL_2G: "2g",
    CELL_3G: "3g",
    CELL_4G: "4g",
    CELL:"cellular",
    NONE: "none"
    };
    

    there may be one more reason for this problem i.e ngCordova is firing up before the device is ready.