【问题标题】:Fİrebase document ref path does not getting datasFİrebase 文档参考路径未获取数据
【发布时间】:2021-07-22 17:44:16
【问题描述】:

我有一个基本的 firebase 问题,你能帮我吗?

我的火力基地是这样的: User > doc(userid) > user information and other collection posts > doc (doc of posts) > post content etc.

现在我有一个collectionGroup:

  const postRef = useFirestore().collectionGroup("posts");

  const { status, data } = useFirestoreCollection(postRef);

我想使用此收藏组返回。 我的意思是现在我们正在收集帖子,但我想回去,所以我想去用户部分。我怎样才能做到这一点? Usign reference.path 可能吗?

我的代码:

{data?.docs?.reverse().map((d, index) => {
                return (
                  <>
                    <div className="d-flex justify-content-center">
                      <div className="post__body align-items-center">
                        <div className="post__header">
                          <div
                            className="post__avatar d-flex"
                            style={{
                              padding: "10px",
                              marginLeft: "-10px",
                            }}
                          >
                            <Avatar src={img1} />
                            <h3 className="profile-post-username">
                             {console.log(d.ref.path)}{" "}
                            </h3>
                          </div>
                          <div className="post__headerDescription asd">
                            <div className="">{d.data().content}</div>
                          </div>
                        </div>
                        <img src={d.data().imageURL} alt="" />
                        <div className="post__footer">
                          <FavoriteBorder fontSize="medium" />
                          <Publish fontSize="medium" />
                        </div>
                      </div>
                    </div>

                    <hr></hr>
                  </>
                );
              })}

如果我们查看控制台,它会给出:

users/NXh8KBd3OUQA4s8048ADy1KqKn33/posts/riruD1DedJzSmoM
users/AhFDgUrP0oWRNgexbvM6sOuNMAu2/posts/stsMitLGk8VKwHC

现在,使用这些信息,我想获取用户的电子邮件或用户名等。

这里主页:

如果我这样做,它会为用户提供 doc id: [![在此处输入图片描述][3]][3]

更新的代码 v2: 现在我创建新的 jsx 文档。它的名称用户信息。 这里:

const UserInfos = () => {
  const userRef = useFirestore().collection("users");

  const { status, data } = useFirestoreCollection(userRef);

  return (
    <div
      className="post__avatar d-flex"
      style={{
        padding: "10px",
        marginLeft: "-10px",
      }}
    >
      <Avatar src={img1} />
      <h3 className="profile-post-username">
        {data?.docs?.map((index) => index.data().email)}{" "}
      </h3>
    </div>
  );
};

现在,主页代码如下:

 {data?.docs?.reverse().map((d, index) => {
                return (
                  <>
                    <div className="d-flex justify-content-center">
                      <div className="post__body align-items-center">
                        <div className="post__header">
                          <UserInfos/>
                          <div className="post__headerDescription asd">
                            <div className="">{d.data().content}</div>
                          </div>
                        </div>
                        <img src={d.data().imageURL} alt="" />
                        <div className="post__footer">
                          <FavoriteBorder fontSize="medium" />
                          <Publish fontSize="medium" />
                        </div>
                      </div>
                    </div>

                    <hr></hr>
                  </>
                );
              })}

这里是截图:

firestore 结构:(帖子是集合)

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore


    【解决方案1】:

    您正在DocumentReference 上寻找parent 属性:

    const data = "" // assume this as your querySnapshot
    
    const postUser = data.docs[0].ref.parent.parent
    // This should be reference to that post's user.
    
    // To get that user's document
    const userDoc = await postUser.get()
    

    要获取帖子的用户信息,您可以编写一个以文档快照为参数的函数:

    async function getUserInfo(reference) {
      const userDoc = await reference.parent.parent.get()
      return userDoc
    }
    
    getUserInfo(d.ref)
    

    也可以看看API call inside React Map

    理想情况下,我会在获取帖子后立即获取用户文档:

    const postRef = useFirestore().collectionGroup("posts");
    
    const data = ""  // The querySnapshot
    const postDocs = data.docs.map(d => d.data())
    
    const userDocs = await Promise.all(data.docs.map(d => d.ref.parent.parent.get()))
    
    userDocs.forEach((uDoc, i) => {
      postDocs[i] = uDoc.data()
      // Returned values will be in order of the Promises passed, regardless of completion order in Promise.all()
    })
    

    【讨论】:

    • 如何在我的代码中调整这些代码(因为我的代码是地图)。你可以把我的代码想象成社交媒体卡,每个用户都有帖子,我可以找到帖子,但我无法获取帖子所有者
    • @efesahin 你想获取所有帖子所有者的文件吗?
    • 我编辑了我的代码,看上面我在下面分享了一张图片。我想要发布内容的用户
    • 我还添加了 1 个屏幕截图。如果我做 d.ref.parent.parent.id ,那么它会给我用户文档 ID,但我想要用户文档信息,如电子邮件 uid 等
    • 我们这样解决: const userRef = useFirestore().collection("users").doc(d.ref.parent.parent.id);这是一个用户文档,当我们使用它时,我们应该使用这个代码 data.data()。之后它给了我们答案
    猜你喜欢
    • 2016-07-09
    • 2022-01-05
    • 2020-12-22
    • 2020-12-09
    • 2018-06-16
    • 1970-01-01
    • 2022-07-01
    • 2021-05-31
    相关资源
    最近更新 更多