[solved] getContactsAsync returning nothing in async thunk on Android

  1. SDK Version: 38.0.8
  2. Platforms(Android/iOS/web/all): Android

Hi there, I’ve used Expo Contacts and getContactsAsync successfully before, but now it’s not returning anything. The only things I’m doing differently this time are using it inside of a Redux thunk and using it with the newly released Android 11. Here’s my code:

import * as Contacts from 'expo-contacts';
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import sortContacts from '../components/SortContacts';

export const fetchContacts = createAsyncThunk('contacts/fetchContacts', async () => {
    const { status } = await Contacts.requestPermissionsAsync(); 
    if (status === 'granted') {
        const { contacts }: any = await Contacts.getContactsAsync().catch((error) => {console.error(error)});
        return(sortContacts(contacts));
    }
});

Using console.log, I’ve determined that the status is correctly returned as 'granted', but 'contacts' is always void.

I’ve looked through all relevant forum topics but haven’t found a solution. Any help would be much appreciated!

Edit: I’ve tried running it on older versions of Android on an emulator and still get the same result. Evaluating 'contacts' results in 'undefined is not an object'.

Fixed it! Changing the syntax to the following worked (not entirely sure why though).

const contacts: any = await Contacts.getContactsAsync().catch((error) => {console.error(error)});
return(sortContacts(contacts.data));

Hi

The fix was needed because your original code was expecting the result to look like this:

{
  contacts: {
    data: [contact1, contact2, ...],
    hasNextPage: false,
    hasPreviousPage: false
  },
  maybe: "something else",
  etc: "etc"
}

when it is actually something like this:

{
  data: [contact1, contact2, ...],
  hasNextPage: false,
  hasPreviousPage: false
}
2 Likes

Thank you! That didn’t make sense at first but now it does :+1:

1 Like

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