Getting count of contacts correct but getting null values for contacts in Ionic 4

戏子无情 提交于 2020-02-23 08:07:25

问题


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)
      })

});

回答1:


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]);
       }
    }
  }
}


来源:https://stackoverflow.com/questions/54647425/getting-count-of-contacts-correct-but-getting-null-values-for-contacts-in-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!