navigator.connection is undefined

4,512 views
Skip to first unread message

Jérémy Sabourin

unread,
May 20, 2014, 10:15:15 AM5/20/14
to phon...@googlegroups.com
Hello, I've got this code and this error :

          // Wait for Cordova to load
          // 
          document.addEventListener("deviceready", onDeviceReady(), false);
          
          // Cordova is loaded and it is now safe to make calls Cordova methods
          //
          var conn;
          function onDeviceReady() {
              alert('Device is ready');
              conn=checkConnection();
              alert(conn);
          }
          
          function checkConnection() {
              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';
          
              alert('Connection type: ' + states[networkState]);
              return networkState;
          }



--> Uncaught TypeError: Cannot read property 'type' of undefined

I saw this trouble on many forums but I tried what I found and it still doesn't work T_T

Thank you !

Kaan Soral

unread,
May 20, 2014, 11:19:05 AM5/20/14
to phon...@googlegroups.com
Which cordova version do you use?

Did you add the network plugin?

John Wargo

unread,
May 20, 2014, 11:20:48 AM5/20/14
to phon...@googlegroups.com
Does the alert fire? Do you see the "Device is ready" alert dialog?

Jérémy Sabourin

unread,
May 21, 2014, 3:16:39 AM5/21/14
to phon...@googlegroups.com
I use cordova 3.4, I tried to delete and re-install the plugin, I can see him when I do "cordova plugins" and I saw in the folders that it's well installed.

The alert('Device is ready') is displayed but not the second one.

Jérémy Sabourin

unread,
May 22, 2014, 7:02:30 AM5/22/14
to phon...@googlegroups.com
No ideas ? or any other way to detect the connection of the device ?
Thank you

John Wargo

unread,
May 22, 2014, 5:30:52 PM5/22/14
to phon...@googlegroups.com
Just for grins, did you try:

var networkState = navigator.network.connection.type;

Anyway, when the object's undefined it's simply because it's not there. I've seen this before when using an older version of node/npm. Make sure you're running the latest version of git, node and Cordova.

Jérémy Sabourin

unread,
May 23, 2014, 3:31:47 AM5/23/14
to phon...@googlegroups.com
Yes I've tried, same result, I think I will ping google.com and see if it is a success or a failure, so it is like a connection test isn't it ?

Jérémy Sabourin

unread,
May 23, 2014, 5:16:19 AM5/23/14
to phon...@googlegroups.com
-_- I can't ping google.com, there is some trouble with cross domain access... Why it can't be simple to check a connection ? 
Cordova is hell !

Kaan Soral

unread,
May 23, 2014, 5:25:13 AM5/23/14
to phon...@googlegroups.com
Please take a step back and first learn what cordova is

You obviously don't know how to add plugins, how to check whether plugins exist and how to configure cordova

Read the documents first: http://cordova.apache.org/docs/en/3.4.0/index.html

You should see the network plugin with the "cordova plugins ls" command - or add it if it doesn't exist

Jérémy Sabourin

unread,
May 23, 2014, 5:45:57 AM5/23/14
to phon...@googlegroups.com
I can see the plugin, and if I want to add it again it tells me that it already exists, so the trouble seems not to be here.
So it should be my code, but I linked it and neither you and I saw any problem, did you ?
That's why I'm growing quite angry, what I need seems to be simple but I spent 3 days on it xD

Kaan Soral

unread,
May 23, 2014, 5:47:22 AM5/23/14
to phon...@googlegroups.com
What other plugins do you have?

What's the "cordova -v"?

Jérémy Sabourin

unread,
May 23, 2014, 6:48:53 AM5/23/14
to phon...@googlegroups.com
I have just it and socialsharing

"cordova -v" displays 3.4.0-0.1.3

I don't understand what just happened : I tested the app on my tab, and it worked...only once. I tried launching app again and navigator.connection.type is always '0', I tried again with or without Wifi, same result, I re-installed the app, same result... are you kidding me cordova ? ^^

Kaan Soral

unread,
May 23, 2014, 7:45:44 AM5/23/14
to phon...@googlegroups.com
I would suggest running "npm install cor...@3.4.1-0.1.0 -g" and re-creating/building a fresh app

3.4.1-0.1.0 is pretty stable and the latest stable version (Its best to re-create the app with the new version to make sure it uses the new version)

Kaan Soral

unread,
May 23, 2014, 7:54:44 AM5/23/14
to phon...@googlegroups.com
Also it might be a good idea to add the device plugin too, just in case

Jérémy Sabourin

unread,
May 23, 2014, 8:14:36 AM5/23/14
to phon...@googlegroups.com
I made the update, I made a new project and I installed the plugin. my index.html is here :

<!DOCTYPE html>
<html>
    <head>
      <script src="cordova.js"></script>
      <script>
        try{
        alert(navigator.connection.type);
        }
        catch(e){
         alert("error : "+e);
        }
      </script>
    </head>
    <body>
    </body>
</html>


And I have the same error... what do you call device plugin ?
Message has been deleted

Jérémy Sabourin

unread,
May 23, 2014, 8:22:51 AM5/23/14
to phon...@googlegroups.com
Okay I installed it, and add ondeviceready in my index.html : 

<!DOCTYPE html>
<html>
    <head>
      <script src="cordova.js"></script>
      <script>
        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            try{
              alert(navigator.connection.type);
            }
            catch(e){
              alert("error : "+e);
            }
        }
        
      </script>
    </head>
    <body>
    </body>
</html>

it finally worked, so the bug comes from my code, maybe from another plugin or another script, I'll give you further informations later

Jérémy Sabourin

unread,
May 23, 2014, 10:09:30 AM5/23/14
to phon...@googlegroups.com
Ok so it's kind of strange but it seems to come from the "app.initialize()" which I put after the external scripts, when I deleted it, it worked...
This bug took me 3 days, it is my personnal best xD
Thank you !

Kaan Soral

unread,
May 23, 2014, 10:19:06 AM5/23/14
to phon...@googlegroups.com
Np :)
Reply all
Reply to author
Forward
0 new messages