【发布时间】: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>
</>
);
})}
【问题讨论】:
标签: javascript reactjs firebase google-cloud-firestore