Ad

Getting Count Of Contacts Correct But Getting Null Values For Contacts In Ionic 4

I am using ionic contacts plugin it is giving me the correct number of contacts in the phone but all the details field are null,

I am using, “cordova-plugin-contacts”: “3.0.1”, “@ionic-native/contacts”: “^5.0.0”

import { Contacts } from '@ionic-native/contacts/ngx';

constructor( private contactsPlugin: Contacts ) { }

document.addEventListener("deviceready", () => {

    this.contactsPlugin.find(["displayName", "phoneNumbers","photos"], {multiple: true, hasPhoneNumber: true})
      .then((contacts) => {
        console.log("Length: ", contacts.length);
        for(let i=0; i < contacts.length; i++){
          console.log('... ',contacts[i]);
          if ( contacts[i].displayName != null ) {
            console.log("--> ", contacts[i].displayName );
          }
        }
      }, 
      (error) => {
        console.error("Contacts error: ", error)
      })

});
Ad

Answer

Solved it like this:

import { Contacts } from '@ionic-native/contacts/ngx';

declare var navigator: any;

@Component({
  selector: 'assignment',
  templateUrl: './assignment.page.html',
  styleUrls: ['./assignment.page.scss']
})

async getContacts() {
 await document.addEventListener("deviceready", () => {

  navigator.contacts.find(
    [navigator.contacts.fieldType.displayName],
    gotContacts,
    errorHandler);

    function errorHandler(e) {
      console.log("errorHandler: "+e);
    }

    function gotContacts(deviceContacts) {
    console.log("gotContacts, number of results " + deviceContacts.length);
      for(var i=0, len = deviceContacts.length; i<len; i++) {
            console.log("Contact >>>  ", deviceContacts[i]);
       }
    }
  }
}
Ad
source: stackoverflow.com
Ad