androidreact-nativehyperlinkexporeact-native-contacts

undefined is not an object (evaluating '_reactNativeContacts.default.getAll')


I am trying du use react-native-contacts with a react-native app made with Expo, but I have this error message :

undefined is not an object (evaluating '_reactNativeContacts.default.getAll')

Here is the code I use :

import React from 'react';
import {
  Image,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
  Modal,
  TouchableHighlight,
  ImageBackground,
  TextInput,
  Picker,
  PermissionsAndroid
} from 'react-native';
import { WebBrowser } from 'expo';
import Contacts from 'react-native-contacts';

import { MonoText } from '../components/StyledText'; 


  Contacts.getAll((err, contacts) => {
    if (err === 'denied'){
      // error
    } else {
      // contacts returned in Array
    }
  })

I tried to follow all he steps for the installation in this page for the android part : https://github.com/rt2zz/react-native-contacts#getting-started

But I don't find where I can do this part : I don't know where I can find this file : android/settings.gradle

By the way I tried this command "react-native link" in my app directory and nothing changed.

Android
In android/settings.gradle
...
include ':react-native-contacts'
project(':react-native-contacts').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-contacts/android')
In android/app/build.gradle
...
dependencies {
    ...
    implementation project(':react-native-contacts')
}

Has anyone had this kind of problem ? Thanks for help !


Solution

  • As far as I understand, you are developing your app with Expo. Some of independent libraries doesn't work well with Expo. I have two suggestions for you.

    1. If you want to keep using react-native-contacts, you need to eject your app from Expo
    2. Or directly use Expo's contacts api, You can find the details in this link Expo's Contacts I would do this which is less work for you to do and solve your problem

      import { Contacts } from 'expo';
      
      const { data } = await Contacts.getContactsAsync({
          fields: [Contacts.Fields.Emails],
      });
      
      if (data.length > 0) {
          const contact = data[0];
          console.log(contact);
      }
      

    You can find same issue created in react-native-contacts github page . Issue