1

according to this link I used react-native-contact to open contact list in android device so the user can choose one and and add it to list. but when I click a button to open a contact list, react-native-contacts getAll returns null. here is a code that I use:

 openContactlist() {


Contacts.getAll((err, contacts) => {
  if (err) {
   // throw err;
    alert("NO");
  }
  // contacts returned
  alert("yes");

})

 }

in render of react native code there is a button that by clicking on it it call above function:

<Button
                title="From contact list"
                onPress={this.openContactlist}
              />

error:

 null is not an object (evaluating '_react-native-contacts.getall')

enter image description here

Saeedeh
  • 297
  • 1
  • 4
  • 21
  • Possible duplicate of [React Native: Possible unhandled promise rejection](https://stackoverflow.com/questions/38842499/react-native-possible-unhandled-promise-rejection) – Karan Harsh Wardhan Mar 27 '19 at 06:26

2 Answers2

2

One reason could be you didn't link the react-native-contact properly

You must also follow these steps to link-

For Android: https://github.com/rt2zz/react-native-contacts#android

For IOS: https://github.com/rt2zz/react-native-contacts#ios

Rahul Saini
  • 216
  • 1
  • 16
  • Try this console.log(Contacts) and you will find out if you linked that properly and also the exact location of getAll in Contact object. – Rahul Saini Mar 28 '19 at 08:12
  • This might not always be the problem, but it was for me, thanks! – kingPuppy Feb 19 '20 at 22:21
  • In my case, I forgot to add the following permission to the AndroidManifest.xml file: Without this permission, you get null when you call "getAll" – Got99Errors Jul 25 '21 at 16:05
2

Follow the steps mentioned in the documentation of react-native-contacts, and add a catch block at the end, it worked for me.

PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
            {
              'title': 'Contacts',
              'message': 'This app would like to view your contacts.'
            }
          ).then(() => {
            con.getAll((err, contacts) => {
              if (err === 'denied'){
                // error
              } else {
                // contacts returned in Array
                console.log(contacts);
              }
            })
          })
          .catch((err)=> {
              console.log(err);
          })