Javier
Javier

Reputation: 320

React-Native-Contacts: Cannot read property 'getAll' of null

I am following this blog to set up react-native-contacts. After following the steps when I try to get all contacts

    Contacts.getAll().then((contacts) => {
      console.log(contacts)
    })

I get this error.

TypeError: Cannot read property 'getAll' of null

For some reason importing Contacts from react-native-contacts appears to be null?

I am not sure if there is an issue with my set up. I followed up by reading over react-natives-contacts set up instructions.

PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
  {
    'title': 'Contacts',
    'message': 'This app would like to view your contacts.',
    'buttonPositive': 'Please accept bare mortal'
  }
)
  .then(Contacts.getAll()
    .then((contacts) => {
        // work with contacts
          console.log(contacts)
        })
          .catch((e) => {
              console.log(e)
          }))

-----------------------------

// in AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS" />

I am using RN 0.69 so auto linking should be enabled? not sure what am I missing?

Upvotes: 0

Views: 1404

Answers (1)

Javier
Javier

Reputation: 320

I am pretty new to react-native/mobile development so this may be obvious or not but I figured out the problem.

  1. I am using VS Code and simply adding the key and string to the info.plist file is NOT what you want to do. I was manually adding
<key>Privacy - Contacts Usage Description</key>
<string>insert message about accessing contacts...</string>

What you WANT to do is open up Xcode and add the key and string from the drop down selecting Privacy - Contacts Usage Description and adding the string within info.plist

<key>NSContactsUsageDescription</key>
<string>insert message about accessing contacts...</string>
  1. I run metro to run the bundle and test on the simulator. Should have rebuilt the package within Xcode or the command line running $ npx react-native start run-ios . Probably a noob mistake but there is my solution. Successfully accessing contacts now👍🏻

Upvotes: 1

Related Questions