user2447161
user2447161

Reputation: 277

ionicframework import module, variable always undefined

I'm trying to import new features inside my ionicframework project. I executed the following command lines :

$ ionic cordova plugin add cordova-plugin-uniquedeviceid

$ npm install --save @ionic-native/unique-device-id

in my file MyOwnService.ts, I'm importing it :

import { UniqueDeviceID } from '@ionic-native/unique-device-id';

import { Http } from '@angular/http';

And then in the constructor of my MyOwnService :

  constructor(http: Http, uniqueDeviceID: UniqueDeviceID) {
      console.log('Loading provider');
      uniqueDeviceID.get()
          .then((uuid: any) => console.log("UID : " + uuid))
          .catch((error: any) => console.log("UIDERR : " + error));
  }

But uniqueDeviceID is always undefined. Also http´ doesn't work anymore if I'm adding an extra parameter in the constructor.

Am I missing something ? Do I have to add something in the app.module.ts ?

I verified the dir exists in workspacedir/node_modules/@ionic-native/unique-device-id´

Upvotes: 1

Views: 353

Answers (1)

Suraj Rao
Suraj Rao

Reputation: 29635

Must UniqueDeviceID be a provider of a provider ?

Yes. From Ionic-Native 3.x onwards all ionic native services need to be set as provider. Check here.

@NgModule({
  ...

  providers: [
    ...
    UniqueDeviceID
    ...
  ]
  ...
})
export class AppModule { }

Secondly, when accessing the plugin, you need to use this.platform.ready() like below:

 constructor(http: Http, uniqueDeviceID: UniqueDeviceID) {
      console.log('Loading provider');
      this.platform.ready().then(() => {
         uniqueDeviceID.get()
          .then((uuid: any) => console.log("UID : " + uuid))
          .catch((error: any) => console.log("UIDERR : " + error));
      });
  }

Also ensure you are testing in an emulator/device as cordova plugins are not enabled when using ionic serve.

Upvotes: 1

Related Questions