简体   繁体   English

无法嵌套 firebase/firestore 查询的 map 数据,React & Typescript

[英]Unable to map data of nested firebase/firestore query, React & Typescript

I am trying to render data of sections and lectures within sections by first fetching data from firestore where lectures are also saved within sections.我试图通过首先从 firestore 中获取数据来呈现部分和部分内的讲座数据,其中讲座也保存在部分内。 My approach is to first query the section collection and then query its lectures collection using the section id.我的方法是先查询section 集合,然后使用section id 查询它的lectures 集合。

In the console I can see the correct outputs for both section data and the nested lecture data.在控制台中,我可以看到部分数据和嵌套讲座数据的正确输出。 On the webpage I don't get the nested lecture data shown, but when saving code changes they sometimes flash for 0.1 seconds.在网页上我没有显示嵌套的讲座数据,但在保存代码更改时,它们有时会 flash 持续 0.1 秒。

I would assume that this points to a problem with rerendering due to the asynchronous nature of the nested query, but I find async functions very confusing and cannot seem to find a way to make them run as I would expect.我假设这指出了由于嵌套查询的异步性质而导致的重新呈现问题,但我发现异步函数非常混乱,而且似乎无法找到一种方法让它们按我预期的方式运行。

import React, {useState, useEffect} from 'react'
import { db } from '../../../firebase-config'
import {collection, onSnapshot, query} from 'firebase/firestore'

type CourseSections =  CourseSection[]

type CourseSection = {
  id: string,
  title: string,
  lectures: CourseLectures,
}

type CourseLectures = CourseLecture[]

type CourseLecture = {
  title: string,
  id: string,
}

const Test2:React.FC = () => {

const [data, setData] = useState<CourseSections>([])

useEffect(()=>{
   async function fetchData() {
      const q = query(collection(db, "products", "5yzLggfe5YC0PAM49enW", "sections"))
      onSnapshot(q, (snapshot)=> {
         let tempSections:CourseSections = []
         snapshot.docs.forEach((doc:any)=>{
            const w = query(collection(db, "products", "5yzLggfe5YC0PAM49enW", "sections", doc.id, "lectures"))
            let tempLectures:CourseLectures = [];
            onSnapshot(w, (snapshotB:any)=>{
               snapshotB.docs.forEach((doc:any)=>{
               tempLectures.push({...doc.data(), id: doc.id})
               })
            })
            tempSections.push({...doc.data(), id:  doc.id, lectures: tempLectures}) //
         })
         setData(tempSections)
      })
   }
   fetchData();
}, [])

return <>
   {data.map((section:CourseSection)=>{
      return <><h2>{section.title}</h2>
         {section.lectures.map((lecture:CourseLecture)=>{
            return <h4>{lecture.title}</h4>
         })}
      </>
   })}
</>
}
export default Test2

I ended up finding a solution to my problem after reading the answer to the following post:在阅读以下帖子的答案后,我最终找到了解决问题的方法:

Is there a way to map a sub-collection into a struct while querying the main collection? 有没有办法在查询主集合时将 map 子集合转换为结构? (Firestore) (消防站)

So instead of querying a collection and immediately querying its subcollections I am now saving the data from the subcollections in a field that contains an array of maps.因此,我现在不是查询集合并立即查询其子集合,而是将子集合中的数据保存在包含地图数组的字段中。 This way I can access all the data in one query and don't need nested queries.这样我就可以在一个查询中访问所有数据,而不需要嵌套查询。

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

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