Cannot read property `getContacts` of undefined
See original GitHub issueI feel like I must be doing something wrong here.
To import react-native-unified-contacts, at the top of the file, I have:
var Contacts = require('react-native-unified-contacts');
(which is copy/pasted straight out of the readme)
In the code, I run:
Contacts.getContacts( (err, contacts) => {
if (err) {
return reject(err);
}
resolve(contacts);
});
Running Contacts.getContacts throws the error at the bottom. It’s as though Contacts is undefined and so the module isn’t exported?
I’ve followed the other instructions in the README - adding the folder into the project in XCode, ensuring it’s a top-level folder, npm install, etc. It’s possible I did that wrong, but I checked twice.
Stacktrace:
Possible Unhandled Promise Rejection (id: 0):
Cannot read property 'getContacts' of undefined
TypeError: Cannot read property 'getContacts' of undefined
at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:84737:9
at tryCallTwo (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25983:1)
at doResolve (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26138:9)
at new Promise (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26004:1)
at InviteFriends.getContacts (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:84735:8)
at new InviteFriends (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:84772:7)
at ReactCompositeComponentWrapper._constructComponentWithoutOwner (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:18187:19)
at ReactCompositeComponentWrapper._constructComponent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:18169:13)
at ReactCompositeComponentWrapper.mountComponent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:18081:15)
at Object.mountComponent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:16531:29)
This is running in the simulator on iOS 10 in XCode 8.
Issue Analytics
- State:
- Created 7 years ago
- Comments:42 (27 by maintainers)
Top Results From Across the Web
Cannot read property 'getContactsAsync' of undefined, expo ...
On my expo ejected project, im trying to use expo-contacts with no success. ... When function getContact is executed? ... the problem was...
Read more >Cannot read property 'getContactsAsync' of undefined, expo ...
On my expo ejected project, im trying to use expo-contacts with no success. "react-native": "~0.61.5", "expo": "~37.0.3", i ran expo install ...
Read more >TypeError: Cannot read property 'find' of undefined - Ionic Forum
Hi,. i am try to do a sample app to read contacts list from phone. But when i call the method:$cordovaContacts.find( i get...
Read more >Typeerror: Cannot Read Property 'Contacts' Of Undefined
Ask questionsCannot read property 'getContacts' of undefined. I'm using the following: Contacts.getContacts( (error, contacts) > { if (error) { console.error( ...
Read more >Access Device's Contact List in React Native App
How to access contact list from the device using React Native App. Accessing contact list for Android and iOS can be made simple...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@jignamru Okay, I was able to get it working without needing to update the library. Here’s what I did:
Create a new app called ExampleApp:
Go into that the new ExampleApp directory:
Install React Native Unified Contacts and save it to
package.json:Open up the ExampleApp in XCode:
Takes a minute to index your new project.
Click on the top level Project:
Click on the
+button in the bottom left and click “Add files…”Navigate to
ExampleApp/node_modules/react-native-unified-contacts/:Click on
RNUnifiedContactsdirectory and clickAdd:Ensure
RNUnifiedContactsis at the first level in my Project Structure:Click on root level
ExampleAppin the project structure:Click on
Build Settingstab:Set the
Use Legacy Swift Language VersiontoNo.Build the project:
This should be “Successful”.
Add
Note: After entering “NSContactsUsageDescription” and tabbing from the key field, it will be replaced with XCode’s friendlier version.
NSContactsUsageDescriptionkey toInfo.plist:Open up
index.ios.js:Add the following code that Includes the
react-native-unified-contactslibrary and gets all the Contacts:Run the project:
This will take a little while to boot up the simulator if you haven’t already got it running.
You’ll see iOS ask you for access to Contacts. Click OK:
Use the Shake Gesture for the Simulator:
Click “Debug JS Remotely”:
Go to the browser window where the remote debugger is attached and view the console, you’ll see that all of the simulator’s Contacts are there:
That should do it!
So try and replicate these steps and see if you can get it working. I’ll be around tonight so post a reply and let me know how it goes.
Well, I’ve got it working in my project, so there IS a way. I’ll just try and reverse engineer it. I won’t be able to test against 8.1 beta but I’ll see what’s going on.
I wish I just wrote this in Obj-C from the get-go but I wanted to make use of the Swift niceties. I might still rewrite in Obj-C just so it plays nicely with React Native and rnpm, though.