How to get all my contacts list


#1

Hello,
How can I get my contacts list?

I tried this:

  findContacts() {
        this.options = new ContactFindOptions();
        this.options.filter = "";
        this.options.multiple = true;
        this.fields = ["displayName"];
        this.nav.contacts.find(this.fields, this.contactfindSuccess, this.contactfindError, this.options);
        
  }
    
    contactfindSuccess(contacts) {
      for (this.i = 0; this.i < contacts.length; this.i+=1) {
         alert("Display Name = " + contacts[this.i].displayName);
      }
  }
	
   contactfindError(message) {
      alert('Failed because: ' + message);
  }

I got this errors:

Error TS2305: Module ‘"/home/ubuntu/workspace/WriteNow/node_modules/ionic-angular/index"’ has no exported member ‘ContactFindOptions’.

Error TS2339: Property ‘contacts’ does not exist on type ‘NavController’.

Thank you,
sagi


#2

hi, have you solved this problem… I am also stuck on this, cannot list contacts


#3

That works fine :slight_smile:

import { Component } from '@angular/core';
import { NavController, MenuController, AlertController} from 'ionic-angular';
import { Contacts, ContactFieldType, ContactFindOptions } from 'ionic-native';
import { Http, Headers, RequestOptions } from '@angular/http';
import { Storage } from '@ionic/storage';
import * as globalvars from '../../../app/globalvars';

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

  public allContacts: any

  /**
  * @param {NavController}  public navCtrl
  * @param {MenuController} public menuCtrl
  */
  constructor(public navCtrl: NavController, public menuCtrl : MenuController, public storage: Storage, public http: Http, public alertCtrl: AlertController) {

    Contacts.find(['displayName', 'name', 'phoneNumbers', 'emails'], {filter: "", multiple: true})
    .then(data => {
      this.allContacts = data
    });

  }
}

#5

Hi, I tried this code on my first application and while I’m fetching the contacts to a list, it just shows [object object]. Doesn’t show any contact name or phone number.

home.ts
getContacts(){
this.contacts.find([‘displayName’, ‘name’, ‘phoneNumbers’], {filter: “”, multiple: true})
.then(data => {
this.allContacts = data

});

html
<button ion-button (click) =“getContacts()”>Add Contacts


<ion-item *ngFor=“let contact of allContacts”>
{{ contact }}


#6

may be you have to use json.parse orjson.stringnify method to get contacts data


#7

Tried it but doesn’t work. Still shows [Object] instead of actual output.


#8

That’s because a Contact is an object. You would need to specify what you want to display.

{{ contact.displayName }}

Or

{{ contact.name.givenName }}

Etc