Problem to use ngCordova : "device is not defined"

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.

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ā€;
}

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.

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

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:

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.

1 Like

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.

1 Like

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.

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.

1 Like

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

1 Like

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:

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.

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.

1 Like