question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property `getContacts` of undefined

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:42 (27 by maintainers)

github_iconTop GitHub Comments

3reactions
joshuapintercommented, Oct 11, 2016

@jignamru Okay, I was able to get it working without needing to update the library. Here’s what I did:

  1. Create a new app called ExampleApp:

    $ react-native init ExampleApp
    
  2. Go into that the new ExampleApp directory:

    $ cd ExampleApp/
    
  3. Install React Native Unified Contacts and save it to package.json:

    $ npm install --save react-native-unified-contacts
    
  4. Open up the ExampleApp in XCode:

    $ open ios/ExampleApp.xcodeproj/
    

    Takes a minute to index your new project.

  5. Click on the top level Project: screenshot 2016-10-11 16 00 21

  6. Click on the + button in the bottom left and click “Add files…” screenshot 2016-10-11 16 00 30

  7. Navigate to ExampleApp/node_modules/react-native-unified-contacts/: screenshot 2016-10-11 16 05 02

  8. Click on RNUnifiedContacts directory and click Add: screenshot 2016-10-11 16 04 24

  9. Ensure RNUnifiedContacts is at the first level in my Project Structure: screenshot 2016-10-11 16 06 02

  10. Click on root level ExampleApp in the project structure: screenshot 2016-10-11 16 08 36

  11. Click on Build Settings tab: screenshot 2016-10-11 16 08 42

  12. Set the Use Legacy Swift Language Version to No. screenshot 2016-10-11 16 09 58

  13. Build the project: screenshot 2016-10-11 16 07 41 This should be “Successful”.

  14. Add NSContactsUsageDescription key to Info.plist: screenshot 2016-10-11 16 14 13 screenshot 2016-10-11 16 14 40 Note: After entering “NSContactsUsageDescription” and tabbing from the key field, it will be replaced with XCode’s friendlier version.

  15. Open up index.ios.js:

    $ open index.ios.js 
    
  16. Add the following code that Includes the react-native-unified-contacts library and gets all the Contacts:

    var Contacts  = require('react-native-unified-contacts');
    
    Contacts.getContacts( (error, contacts) =>  {
    if (error) {
      console.error(error);
    }
    else {
      console.log(contacts);
    }
    });
    
  17. Run the project: screenshot 2016-10-11 16 12 02 This will take a little while to boot up the simulator if you haven’t already got it running.

  18. You’ll see iOS ask you for access to Contacts. Click OK: screenshot 2016-10-11 16 20 49

  19. Use the Shake Gesture for the Simulator: screenshot 2016-10-11 16 21 57

  20. Click “Debug JS Remotely”: screenshot 2016-10-11 16 22 30

  21. 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: screenshot 2016-10-11 16 23 04

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.

1reaction
joshuapintercommented, Oct 26, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found