简体   繁体   English

从数组字段中的文档 id 中获取 firestore 文档并在 React 中显示

[英]Fetch firestore document from document id in array field and display in React

I have 2 collections on firestore, boxes contains a field shoes that is an array of reference id to the shoes collections :我在 firestore 上有 2 个集合, boxes包含一个字段shoes ,它是对shoes collections的引用 id 数组:

在此处输入图片说明 在此处输入图片说明

My Boxes component is fetching all boxes and displaying their number.我的盒子组件正在获取所有盒子并显示它们的编号。 I also want to display properties of the shoes in it, like a photo.我还想在其中显示鞋子的属性,例如照片。 So I go about like that:所以我是这样的:

Boxes.jsx Boxes.jsx

 // DEPENDENCIES import React, { useEffect, useContext } from 'react'; // COMPONENTS import BoxCard from '../Components/BoxCard'; // CONTEXT import ShoesContext from '../Contexts/ShoesContext'; // HELPERS import db from '../config/firebase'; let initialBoxes = []; const Boxes = () => { const { boxes, setBoxes } = useContext(ShoesContext); useEffect(() => { initialBoxes = []; db.collection('boxes') .get() .then(querySnapshot => { querySnapshot.forEach(doc => { initialBoxes.push(doc); }); setBoxes(initialBoxes); }); }, []); return ( <div> <h3>You have {boxes.length} boxes:</h3> {!boxes ? ( <div>Loading..</div> ) : ( boxes.map(box => { return <BoxCard box={box} key={box.id} />; }) )} </div> ); }; export default Boxes;

Boxes.jsx Boxes.jsx

 import React from 'react'; import TestComponent from './TestComponent'; const BoxCard = ({ box }) => { const theBox = box.data(); return ( <div> <h5> Box number {theBox.number} has {theBox.shoes.length} shoes:{' '} </h5> {theBox.shoes.map(shoe => { return <TestComponent shoe={shoe} />; })} </div> ); }; export default BoxCard;

and this is where it all breaks:这就是一切破裂的地方:

 import React from 'react'; const TestComponent = ({ shoe }) => { useEffect(() => { let hell; shoe.get().then(doc => { hell = doc.data(); }); }, []); return <div>{hell ? hell.season : 'loading...'}</div>; }; export default TestComponent;

hell is undefined. hell是未定义的。 I have not found a way to render the nested docs so I can use them in my TestComponent component.我还没有找到渲染嵌套文档的方法,因此我可以在我的TestComponent组件中使用它们。 My extensive research online has not been succesful so far, hence my question today.到目前为止,我在网上的广泛研究还没有成功,因此我今天提出问题。

Thanks!谢谢!

Update:更新:

I seem to have found the answer, answer from Josh below put me on the right track.我似乎找到了答案,下面乔希的回答让我走上了正轨。 See below code for TestComponent.jsx :请参阅下面的TestComponent.jsx代码:

 import React, { useEffect, useState } from 'react'; // HELPERS import db from '../config/firebase'; const TestComponent = ({ shoe }) => { const [hell, setHell] = useState(); useEffect(() => { db.collection('shoes') .doc(shoe.id) .get() .then(doc => { setHell(doc.data()); }); }, []); return <div>{hell ? hell.season : 'loading...'}</div>; }; export default TestComponent;

What is shoe in shoe.get()... in the TestComponent?什么是shoeshoe.get()...在TestComponent? Shouldn't it be db.doc(shoe).get()... .不应该是db.doc(shoe).get()...

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

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