react-native-unified-contacts: Cannot read property `getContacts` of undefined

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.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 42 (27 by maintainers)

Most upvoted comments

@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.

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.