未定义不是对象吗?

时间:2018-09-10 07:20:13

标签: javascript reactjs react-native

我收到以下错误:

  

未定义不是对象(正在评估“ item.id”)

我有这个。获取项目并删除项目:

    getItems = async () => {
    this.setState({ refreshing: true });
    this.unsubscribe = await this.ref.onSnapshot((querySnapshot) => {
          const todos = [];
          querySnapshot.forEach((doc) => {
            todos.push({
              id: doc.id,
              tips: doc.data().tips,
              date: doc.data().date,
              user: doc.data().user,
              like: doc.data().like,
            })
            this.ref.get().then(function(documentSnapshot) {
              // check and do something with the data here.
            });
          })
          this.setState({
            refreshing: false,
            getData: todos
          })
        })
  }
  deletePost({ item, index }){
    var deleteItemId = item.id;
     firestore.collection("tips").doc(deleteItemId).delete().then(function() {
          alert("deleted", deleteItemId)
      }).catch(function(error) {
          alert("Error removing document: ", error);
      });
  }

渲染的项目,例如:

renderItem = ({ item, index }) => {

    return (
        <Text onPress={() => this.deletePost(item.id)}>Delete</Text>
    )
 }

这是item.id,就像NnX57tSpqvPvIUN7tRY4

1 个答案:

答案 0 :(得分:2)

似乎有可能undefined时调用了该方法。确保item.id不是undefined

deleteItemId && firestore.collection("tips").doc(deleteItemId).delete()