Saeedeh
Saeedeh

Reputation: 295

react-native-contacts getAll return null

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

Upvotes: 1

Views: 3377

Answers (2)

Ankit Abhinav
Ankit Abhinav

Reputation: 49

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);
          })

Upvotes: 2

Rahul Saini
Rahul Saini

Reputation: 222

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

Upvotes: 2

Related Questions