简体   繁体   English

React-Native Firestore - 获取评论部分的用户信息

[英]React-Native Firestore - Get user info for a comment section

I'm building an app using react-native and react-native-firebase and i'm running into an issue while trying to implement a comment section.我正在使用 react-native 和 react-native-firebase 构建一个应用程序,我在尝试实现评论部分时遇到了问题。

My tree of data is currently like that: collection(comments)/doc(currentUser.uid)/collection(userComments)/doc(commentCreatorID)我的数据树目前是这样的:collection(comments)/doc(currentUser.uid)/collection(userComments)/doc(commentCreatorID)

Within this doc commentCreatorID there is all the data i need.在这个文档 commentCreatorID 中有我需要的所有数据。 So basically the content, a timestamp...所以基本上内容,时间戳......

For this part everything works perfectly but in order to havethe commentCreator's infos stick with his post, i need to grab them somewhere else.对于这部分,一切正常,但为了让评论创作者的信息与他的帖子保持一致,我需要在其他地方抓住它们。 The way i do that is taking the doc(commentCreatorID), as it is the uid of this user and ask firestore to give me the data from the document with this same id within my "users" collection.我这样做的方式是获取 doc(commentCreatorID),因为它是该用户的 uid,并要求 firestore 在我的“用户”集合中向我提供具有相同 ID 的文档中的数据。

Here is my code:这是我的代码:

  const [comments, setComments] = useState([])
  const [commentsReady, setCommentsReady] = useState([])

useEffect(() => {

        setComments([])
        setLoading(true)
        firestore()
        .collection('comments')
        .doc(auth().currentUser.uid)
        .collection('userComments')
        .get()
        .then((snapshot) => {
            let comments = snapshot.docs.map(doc => {
                const data = doc.data()
                const id = doc.id
                return {id, ...data}
            })
            setComments(comments)
        })
        .then(() => {
            comments.forEach(comment => {
                firestore()
                .collection("users")
                .doc(comment.id)
                .get()
                .then((snapshot) => {
                    const data = snapshot.data()
                    setCommentsReady({comments, ...data})       
                })
            })
        })
       console.log(commentsReady)
        setLoading(false)
    }, [handleScroll4])

This doesn't seem to works well as for now.这似乎不像现在那样运作良好。 My log throw an empty array right into my face.. I'm grabbing each comment correctly tho and even each user's data corresponding to their uids.我的日志将一个空数组直接扔到我的脸上。我正确地抓住了每条评论,甚至每个用户的数据都对应于他们的 uid。 I can log them once ForEach have been done. ForEach 完成后,我可以记录它们。 But for some reason i can't have them set to my state commentsReady.但由于某种原因,我无法将它们设置为我的 state 评论就绪。

Did i miss something?我错过了什么?

Thanks for your time谢谢你的时间

The setters that the useState function returns are async. useState function 返回的设置器是异步的。 Copying data from one state to another is also an antipattern.将数据从一个 state 复制到另一个也是一种反模式。 Try using effects.尝试使用效果。

const TT = () => {
  const [comments, setComments] = useState([]);
  const [userInfos, setUserInfos] = useState([]);

  const commentsView = comments.map(comment => {
    // Reactively merge each comment with the appropriate user info
  });

  useEffect(() => {
    firestore()
    .collection('comments')
    .doc(auth().currentUser.uid)
    .collection('userComments')
    .get()
    .then((snapshot) => {
        let comments = snapshot.docs.map(doc => {
            const data = doc.data()
            const id = doc.id
            return {id, ...data}
        });
        setComments(comments);
    });
  }, []);

  useEffect(async () => {
    // Maybe keep a cache of fetched user datas, but not sure how this is architected
    const snapshots = await Promise.all(comments.map(comment => {
      return firestore()
      .collection("users")
      .doc(comment.id)
      .get();
    }));
      setUserInfos(snapshots.map(snap => snap.data()));
    }, [comments]);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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