简体   繁体   中英

Cannot read property 'getContactsAsync' of undefined, expo-contacts, ANDROID

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 expo-contacts

import * as Permissions from 'expo-permissions';
import * as Contacts from 'expo-contacts';

 getContact = async () => {
    try {
      let { status } = await Permissions.askAsync(Permissions.CONTACTS);
        if (status === 'granted') {
          const { data } = await Contacts.getContactsAsync({
            fields: [Contacts.Fields.Emails],
          });
          console.log("getContact / data: ", data);
  
          if (data.length > 0) {
            const contact = data[0];
            console.log("getContact / contact: ", contact);
          }
        }
    } catch(e){
      console.log("getContact / error: ", e)
    }
  }

<TouchableOpacity style={{ marginTop: 20 }} onPress={() => this.getContact()}>
   <Image source={contactsIcon} style={{ width: 30, height: 30 }} />
</TouchableOpacity>

在此处输入图像描述

Have you set up the permissions in your AndroidManifest.xml as follows:

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

Also take a note that: "For bare React Native projects, you must ensure that you have installed and configured the react-native-unimodules [1] package before continuing."

[1] https://github.com/expo/expo/tree/master/packages/react-native-unimodules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM