Problem to use ngCordova : "device is not defined"


#1

Hi,

I tried a lot of solutions I found to solve my issue but probably not the good one. Hope someone could help me :wink:

I launched an Ionic project, and i need to get the UUID.
I installed ngCordova with bower and did all the Install paragraph said here : ngCordova website

Here is my app.js file:

I just added ngCordova at the angular.module line, and added $cordovaDevice in the run method

FYI :

  • I already called the ng-cordova.js script in the index.html file.
  • The device plugin is installed

And here is my console log error :

Uncaught ReferenceError: device is not defined

Does someone have any idea to resolve it ?
Many thanks.


$cordovaDevice plugin not working in Ionic View App
I am new to ionic and cordova... I am trying to read the device UUID but its returning empty dictionary
#2

I place the following snuppet in $ionicPlatform.ready, like you did, i go like this and put my values in a variable that I got called appData.state. but you could put it elsewhere or to the console… hope this helps. :

if( ionic.Platform.isAndroid() ){
appdata.state.deviceModel = $cordovaDevice.getModel();
appdata.state.deviceUUID = $cordovaDevice.getUUID();
}else{
console.log(“Is not Android”);
appdata.state.deviceModel = “windows”;
appdata.state.deviceUUID = “testUUID”;
}


#3

Thank you for your answer boltex.
Unfortunately, your example don’t work for me. That tells me the same thing : device is not defined and i really don’t understand why.

Maybe i forgot something somewhere in the code. Is it possible for you to give me please a little piece of your code that works with ngCordova that i can test ?

Or someone else maybe has another idea ?

Many thanks.


#4

Just curious, did you add ngcordova before or after cordova.js? It does make a difference.


#5

Hi Mike,

I added ngCordova before cordova.js. I reproduced all the step described in the documentation but that didn’t work …
I found a solution to get around my issue but this is not the one i wanted.

I now launch the command phonegap serve rather than ionic serve. I put the .cordova folder (generated with phonegap) at the root of the project generated by ionic. And i don’t use ngCordova anymore, but the “native” plugins give by phonegap.

If you have any solution to help me to use ngCordova with ionic, i’d be delighted to hear :smile:


#6

Alright, soo none of the cordova plugins will work with ionic serve of phonegap serve. They need to be installed on the device in order to work.


#7

yeah like mhartington said: plug in your android phone into a usb port on your computer, enable debug on your phone and type: ionic run android

the code i gave you tests to see if it’s on a phone or on the browser before checking for the uuid.


#8

If you did a custom download of ngCordova, there are several errors in their output that you will need to fix, I got that error because of the module naming being incorrect.


#9

I have the same problem all I am trying to do is
try{
$scope.main.MyUUID=$cordovaDevice.getUUID();
}
catch(err){$scope.main.MyUUID=“test”;
alert(err.message);}
}
And the alert is Device is not defined. Even when I run it on my android phone.

the top is like this to allow ngCordova and $cordovaDevice
angular.module(‘starter.controllers’,[‘ngCordova’])

.controller(‘httpLogin’, [’$scope’,’$http’,‘DSP_URL’,’$ionicLoading’,’$cordovaDevice’, function ($scope,$http,DSP_URL,$ionicLoading,$cordovaDevice) {

and my library loads are in this order
src=“cordova.js”>
src=“lib/ionic/js/ionic.bundle.js”>
src=“lib/ngCordova/dist/ng-cordova.js”>
src=“js/app.js”>
src=“js/controllers.js”>
src=“js/services.js”>

I must be missing a step to make this work.


#10

@alindzon, make sure you run that code after ionicplatform ready :slight_smile:


#11

Hi,
I tried your solution @boltex and that works. I only tried the “ionic serve” command, that’s why that didn’t work.
Many thanks for your help :wink:


#12

offtopic: how do you confirm a selected option of a select control if you call Keyboard.hideAccessoryBar(true)? It seems a bit problematic. We regret to manage a virtual keyboard at all, as many issues are raised after that.


#13

Can you provide a detail way of how you fixed this problem? I have the same ‘device is not defined’ error message when trying to collect the device info.