Search code examples
angularjsionic2ionic-native

ionic 2 native contacts plugin find doesn't exit


I am trying to query the all contacts using ionic 2 native. I got the following error while run the ionic serve command. How can i over come this issue if anyone know let me know please. Thank you

Typescript Error
Property 'find' does not exist on type 'typeof Contacts'.

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {

  }
  testFun(){
    Contacts.find(['*']).then((contacts)=>{
      alert(JSON.stringify(contacts[0]));
    })
  }
}

home.html

<ion-header>
    <ion-navbar>
        <ion-title>
            Ionic Blank
        </ion-title>
    </ion-navbar>
</ion-header>

<ion-content padding>
    <button ion-button full (click)="testFun()">Get Contacts</button>
</ion-content>

Error imageenter image description here

source codeGit repo for my project


Solution

  • Since Ionic 3.x.x, the way you use Native plugin is a little different from Ionic 2.x.x

    1. First you need to add Contactin your constructor
    2. You need to add the providers Contactin your @Component

    So your home.ts should looks like this instead:

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html',
      providers: [Contacts]
    })
    export class HomePage {
    
      constructor(public navCtrl: NavController, private contacts: Contacts) {
    
      }
    
      testFun(){
        this.contacts.find(['*']).then((contacts)=>{
          alert(JSON.stringify(contacts[0]));
        })
      }
    }
    

    More usage example check the official doc for Ionic 3.x NATIVE doc here http://ionicframework.com/docs/native/contacts/