https://forums.expo.io/t/contacts-getcontactsasync-ignore-my-contactquery-critera-email/15168


#1

My question is identical to this one by @theinternet, which never got a response and was closed.

I am trying to retrieve only certain fields from contacts, but the “fields” property doesn’t do anything.

Trying this:

    const contacts = await Expo.Contacts.getContactsAsync({
      fields: [Expo.Contacts.PHONE_NUMBERS, Expo.Contacts.EMAILS],
      pageSize: 0,
      pageOffset: 0,
      sort: sortType
    });

Returns the entire contact object, not just the phone and emails.

And trying this (using a key naming that matches the returned object):

const contacts = await Expo.Contacts.getContactsAsync({
  fields: [Expo.Contacts.phoneNumbers, Expo.Contacts.emails, Expo.Contacts.lastName],
  pageSize: 0,
  pageOffset: 0,
  sort: sortType
});

Crashes expo.

For what its worth, my sorting object works and is defined as follows:

const sortType = Expo.Contacts.SortTypes.LastName;

I have tried using LastName in my fields as well and it also crashes expo.

Thanks in advance for any help with this.


getContactsAsync not returning all contacts
#2

In case it can save someone some time, here’s the code I used to parse the returned contact object into a simpler one with only the desired fields:

    var contactsCleaned = {};
    contactsCleaned.data = [];
    if (contacts.total > 0) {
      console.time('timerGetContactsAndAddToNewObject');
      console.log('contacts.total:');
      console.log(contacts.total);
      contacts.data.map((contact) => {
        var cleanContact = {};
        cleanContact.firstName = contact.firstName;
        cleanContact.lastName = contact.lastName;
        cleanContact.emails = contact.emails;
        cleanContact.phoneNumbers = contact.phoneNumbers;
        contactsCleaned.data.push(cleanContact);
      });
      console.timeEnd('timerGetContactsAndAddToNewObject');

      console.time('timerLogCleanContacts');
      console.log('contactsCleaned:');
      console.log(contactsCleaned);
      contactsCleaned.data.map((cleanContact) => {
        console.log('cleanContact=');
        console.log(JSON.stringify(cleanContact));
      });
      console.timeEnd('timerLogCleanContacts');

#3

Sorry about the issues you’re running into. Could you please share the device OS and version of Expo?


#4

I’m using expo SDK 30 on iOS 12.1 with Expo 2.9.0.103161.


closed #5

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.