SimCoderYoutube / InstagramClone

Instagram Clone React Native Tutorial 2021 👨‍💻 I'll show you how you can do this in the simplest way and terms possible. By the end of this series you'll have learned how the big companies do it and will be able to do the same, you not only will be able to do this app, but you'll be able to put what you learn into your very own projects! In this series, we use React Native with Expo to quickly deploy the project. We use firebase for all our microservice needs like the auth system, database, storage, amongst others. firebase, redux, react native, javascript, expo. In this series, we'll use all of them and you'll learn them by doing an iconic app. Welcome to this Simcoder project and make an Instagram Clone!
Apache License 2.0
745 stars 294 forks source link

I can't seem to load comments with username attached #38

Open NOxDriver opened 2 years ago

NOxDriver commented 2 years ago

I have the same "comment" page as you:


import { Text, View, FlatList, Button, TextInput, TouchableOpacity } from 'react-native'
import React, { useState, useEffect } from 'react'

import firebase from 'firebase/compat/app'
require('firebase/firestore')

import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { fetchUsersData } from '../../redux/actions/index'

function Comment(props) {
  const [comments, setComments] = useState([])
  const [postId, setPostId] = useState("")
  const [text, setText] = useState(null)

  useEffect(() => {

    function matchUserToComment(comments) {
      for (let i = 0; i < comments.length; i++) {
        if (comments[i].hasOwnProperty('user')) {
          continue;
        }

        const user = props.users.find(x => x.uid === comments[i].creator)
        if (user == undefined) {
          props.fetchUsersData(comments[i].creator, false)
        } else {
          comments[i].user = user
        }
      }
      setComments(comments)
      // setRefresh(false)
    }

    if (props.route.params.postId !== postId) {
      firebase.firestore()
        .collection('posts')
        .doc(props.route.params.uid)
        .collection('userPosts')
        .doc(props.route.params.postId)
        .collection('comments')
        .get()
        .then((snapshot) => {
          let comments = snapshot.docs.map(doc => {
            const data = doc.data();
            const id = doc.id;
            return { id, ...data }
          })
          matchUserToComment(comments)

        })
      setPostId(props.route.params.postId)
    } else {
      matchUserToComment(comments)
    }
  }, [props.route.params.postId, props.users])

  const onCommentSend = () => {
    firebase.firestore()
      .collection('posts')
      .doc(props.route.params.uid)
      .collection('userPosts')
      .doc(props.route.params.postId)
      .collection('comments')
      .add({
        creator: firebase.auth().currentUser.uid,
        text

      })
  }

  return (
    <View>
      <FlatList
        numColumns={1}
        horizontal={false}
        data={comments}
        renderItem={({ item }) => (
          <View style={{ flexDirection: 'row', margin: 10 }}>
            {item.user !== undefined ?
              <Text> {item.user.name}</Text>
              : null}
            <Text>{item.text}</Text>
          </View>
        )}
      >
      </FlatList>
      <View>
        <TextInput
          placeholder='Add Comment'
          onChangeText={(text) => setText(text)}
        />
        <TouchableOpacity>
          <Text onPress={() => onCommentSend()}>Send</Text>
        </TouchableOpacity>
      </View>
    </View >
  )
}

const mapStateToProps = (store) => ({
  users: store.usersState.users
  // chats: store.userState.chats,
  // friendsRequestsReceived: store.userState.friendsRequestsReceived,
})

const mapDispatchProps = (dispatch) => bindActionCreators({ fetchUsersData }, dispatch);

export default connect(null, mapDispatchProps)(Comment);

But when I try and view the comments, I get either " undefined, when evaluating (props.users.find). Or, if I change from users. to user. I then get: [Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'props.user.find')]

Anything obvious that I'm doing wrong?

I can load the comments without the username if I change matchUserToComments to setComments.