react-native-contacts: undefined is not an object

Hi. I’ve done npm install react-native-contacts and react-native link react-native-contacts Scanning 760 folders for symlinks in /home/alexander/projects/AwesomeProject/node_modules (11ms)

Here is my code:

import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { Font } from 'expo';
import Contacts from 'react-native-contacts';
export default class App extends React.Component {
  state = {
    fontLoaded: false
  };
  async componentDidMount() {
    await Font.loadAsync({
      'open-sans-bold': require('./assets/fonts/OpenSans-Bold.ttf')
    });
    this.setState({ fontLoaded: true });
  };
  onPressExport() {
      // var Contacts = require('react-native-contacts');
      // var Contacts = require('./node_modules/react-native-contacts');
      if (window.Contacts || true) {
        if (Contacts.getAll || true) {
            Contacts.getAll((err, contacts) => {
                if (err === 'denied') {
                  // error
                  // error
                  // error
                  console.log('error');
                } else {
                  // contacts returned in []
                  console.log('ok');
                }
            });
        } else {
            console.log('Contacts.getAll == null');
        }
      } else {
          console.log('Contacts == null');
      }
  };
  render() {
    return (
      <View style={styles.container}>
		{
          this.state.fontLoaded ? (
            <Text style={{ fontFamily: 'open-sans-bold', fontSize: 22 }}>
              Привет, мир!
            </Text>
          ) : null
        }
        <Text>Open up App.js to start working on your app!</Text>
        <Text>Changes you make will automatically reload.</Text>
        <Text>Shake your phone to open the developer menu.</Text>
        <Button
          onPress={this.onPressExport}
          title="Export"
          color="#841584"
          accessibilityLabel="Learn more about this purple button"
            />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  baseText: {
    fontFamily: 'open-sans-bold',
	fontSize: 20,
    fontWeight: 'bold'
  }
});

and error:

undefined is not an object (evaluating '_reactNativeContacts2.default.getAll')
onPressExport
    App.js:23:21
touchableHandlePress
    TouchableOpacity.js:129:45
_performSideEffectsForTransition
    Touchable.js:746:34
_receiveSignal
    Touchable.js:664:44
touchableHandleResponderRelease
    Touchable.js:433:24
invokeGuardedCallback
    ReactNativeStack-dev.js:130:19
invokeGuardedCallback
    ReactNativeStack-dev.js:166:43
invokeGuardedCallbackAndCatchFirstError
    ReactNativeStack-dev.js:169:64
executeDispatch
    ReactNativeStack-dev.js:202:128
executeDispatchesInOrder
    ReactNativeStack-dev.js:208:279
executeDispatchesAndRelease
    ReactNativeStack-dev.js:272:58
executeDispatchesAndReleaseTopLevel
    ReactNativeStack-dev.js:276:39
forEachAccumulated
    ReactNativeStack-dev.js:268:37
processEventQueue
    ReactNativeStack-dev.js:340:143
runEventQueueInBatch
    ReactNativeStack-dev.js:637:79
handleTopLevel
    ReactNativeStack-dev.js:642:29
<unknown>
    ReactNativeStack-dev.js:749:51
fiberBatchedUpdates
    ReactNativeStack-dev.js:691:14
performFiberBatchedUpdates
    ReactNativeStack-dev.js:695:31
perform
    ReactNativeStack-dev.js:1382:99
batchedUpdates
    ReactNativeStack-dev.js:2077:139
batchedUpdates$1
    ReactNativeStack-dev.js:1456:61
batchedUpdates
    ReactNativeStack-dev.js:699:31
batchedUpdatesWithControlledComponents
    ReactNativeStack-dev.js:708:30
_receiveRootNodeIDEvent
    ReactNativeStack-dev.js:748:46
receiveTouches
    ReactNativeStack-dev.js:762:60
__callFunction
    MessageQueue.js:266:47
<unknown>
    MessageQueue.js:103:26
__guard
    MessageQueue.js:231:6
callFunctionReturnFlushedQueue
    MessageQueue.js:102:17

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 25 (6 by maintainers)

Most upvoted comments

Something problem in auto link. after manual link it works well.

  1. In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project’s name]
  2. add ./node_modules/react-native-contacts/RCTContacts.xcodeproj
  3. In the XCode project navigator, select your project, select the Build Phases tab and in the Link Binary With Libraries section add libRCTContacts.a

@deepanjan1 I’m not using Expo and I ran into the issue.

I actually resolved it doing this:

I encountered this problem as well. In my case, i installed the react-native-contacts library as described by @apetrichkovich after the iOS simulator is already running. What I discovered is that react-native link react-native-contacts doesn’t actually link the binary in the traditional sense where an executable is built (i.e ld.so ), but rather just specifies it as dependency that needs to be linked in the future . So doing a reload via “cmd+R” wont do anything as it only reloads the javascript file, instead of rebuilding the actual iOS app that’ll contain the new native dependency.

So to remove the error, I simply had to quit iOS simulator and restart it again via react-native run-ios to actually re-build/link the executable. And now, the Contacts module becomes available instead of undefined.

I resolved this by adding some code in MainApplication.java

    protected List<ReactPackage> getPackage() {
        return Arrays.<ReactPackage>asList(
                new ReactNativeContacts()
        );
    }

I’m still getting this problem even after linking, reinstalling…etc. Any ideas?

@apetrichkovich I think the problem with linking. Did you get the success messages after linking? Since this library is a pure native library. If native codes are not linked, It cannot export “Contacts”. That’s why you are getting “undefined is not an object” error.