javascript - 类型错误 : undefined is not an object (evaluating 'item.phoneNumbers[0]' )

标签 javascript reactjs react-native expo

我想在我的应用程序中使用 expo-contacts 呈现我的联系人列表,列表显示大约 2 秒,然后我得到类型错误:未定义不是对象(评估“item.phoneNumbers [0]”)。我已经检查了文档以查看我是否犯了任何错误,但我找不到任何错误。有没有人能解决这个问题

下面是我的代码

联系人列表.js

import React, { Component } from "react";
import {
  View,
  Text,
  Platform,
  StatusBar,
  FlatList,
  StyleSheet,
  ActivityIndicator
} from "react-native";
import * as Contacts from "expo-contacts";
import * as Permissions from "expo-permissions";

class ContactList extends Component {
  static navigationOptions = {
    header: null
  };

  constructor(props) {
    super(props);
    this.state = {
      isLoading: false,
      contacts: []
    };
  }

  async componentDidMount() {
    this.setState({
      isLoading: true
    });

    this.loadContacts();
  }

  loadContacts = async () => {
    const permissions = await Permissions.askAsync(Permissions.CONTACTS);

    if (permissions.status !== "granted") {
      return;
    }

    const { data } = await Contacts.getContactsAsync({
      fields: [Contacts.Fields.PhoneNumbers, Contacts.Fields.Emails]
    });

    this.setState({
      contacts: data,
      isLoading: false
    });
  };

  handleBack() {
    this.props.navigation.goBack();
  }

  renderItem = ({ item }) => (
    <View style={{ minHeight: 70, padding: 5 }}>
      <Text>
        {item.firstName}
        {item.lastName}
      </Text>
      <Text>{item.phoneNumbers[0].digits}</Text>
    </View>
  );

  render() {
    const { isLoading, contacts } = this.state;
    let emptyContact = null;

    emptyContact = (
      <View style={styles.emptyContactStyle}>
        <Text style={{ color: "red" }}>No Contacts Found</Text>
      </View>
    );

    return (
      <SafeAreaView style={styles.contentWrapper}>
        <View style={styles.contentWrapper}>
          {isLoading ? (
            <View style={styles.isLoadingStyle}>
              <ActivityIndicator size="large" color="#2484E8" />
            </View>
          ) : null}
          <FlatList
            data={contacts}
            renderItem={this.renderItem}
            keyExtractor={(item, index) => index.toString()}
            ListEmptyComponent={emptyContact}
          />
        </View>
      </SafeAreaView>
    );
  }
}

最佳答案

这是一个新的答案,因为以前的答案是题外话。出现此错误是因为显示的联系人没有 phoneNumber

您应该先检查电话号码是否存在,然后再显示它:

renderItem = ({ item }) => (
  <View style={{ minHeight: 70, padding: 5 }}>
    <Text>
      {item.firstName}
      {item.lastName}
    </Text>
    <Text>
      {item.phoneNumbers && item.phoneNumbers[0] && item.phoneNumbers[0].digits}
    </Text>
  </View>
);

关于javascript - 类型错误 : undefined is not an object (evaluating 'item.phoneNumbers[0]' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376168/

相关文章:

javascript - CSS :hover/:focus vs click event on (mobile) touch devices

javascript - 使用 JavaScript 在 HTML 中显示 CSS 属性的值

iOS 应用程序 : Rejected in AppStore to external testers

node.js - Haste 模块映射中不存在模块 `fs`

带有 'var' 和没有 'var' 的 javascript 全局变量

javascript - 基本了解javascript中的函数和声明

javascript - 使用hooks时如何为panResponder设置Offset?

javascript - 当我有 allMarkdownRemark 时, Gatsby 一直提示无法在类型 "fields"上查询字段 "MarkdownRemark"

reactjs - 如何使用 nextjs 删除此 Prop 类型警告?

ios - React Native FBSDK 登录 - 使用应用程序登录失败