undefined 不是一个对象(评估'_reactNativeContacts.default.getAll')

undefined is not an object (evaluating '_reactNativeContacts.default.getAll')

我正在尝试将 react-native-contacts 与 Expo 制作的 react-native 应用程序一起使用,但我收到此错误消息:

undefined is not an object (evaluating '_reactNativeContacts.default.getAll')

这是我使用的代码:

import React from 'react';
import {
  Image,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
  Modal,
  TouchableHighlight,
  ImageBackground,
  TextInput,
  Picker,
  PermissionsAndroid
} from 'react-native';
import { WebBrowser } from 'expo';
import Contacts from 'react-native-contacts';

import { MonoText } from '../components/StyledText'; 


  Contacts.getAll((err, contacts) => {
    if (err === 'denied'){
      // error
    } else {
      // contacts returned in Array
    }
  })

我尝试按照此页面中 android 部分的所有安装步骤进行安装: https://github.com/rt2zz/react-native-contacts#getting-started

但我找不到可以做这部分的地方: 我不知道在哪里可以找到这个文件:android/settings.gradle

顺便说一句,我在我的应用程序目录中尝试了这个命令 "react-native link",但没有任何改变。

Android
In android/settings.gradle
...
include ':react-native-contacts'
project(':react-native-contacts').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-contacts/android')
In android/app/build.gradle
...
dependencies {
    ...
    implementation project(':react-native-contacts')
}

有人遇到过这种问题吗? 感谢您的帮助!

据我了解,您正在使用 Expo 开发您的应用程序。一些独立图书馆不能很好地与世博会合作。我有两个建议给你。

  1. 如果您想继续使用 react-native-contacts,您需要从 Expo 中退出您的应用程序
  2. 或者直接使用 Expo 的联系方式 api, 你可以在这个 link Expo's Contacts 中找到详细信息并解决你的问题

    import { Contacts } from 'expo';
    
    const { data } = await Contacts.getContactsAsync({
        fields: [Contacts.Fields.Emails],
    });
    
    if (data.length > 0) {
        const contact = data[0];
        console.log(contact);
    }
    

您可以在 react-native-contacts github 页面中找到相同的问题。 Issue

2021 年 7 月更新

Contacts 模块已从核心 expo 包移至 expo-contacts(参见 documentation)。

示例:

import * as Contacts from 'expo-contacts';

const { status } = await Contacts.requestPermissionsAsync();

if (status === 'granted') {
  const { data: contacts } = await Contacts.getContactsAsync();

  console.log('Retrieved contacts!', contacts);
}