简体   繁体   English

处理侦听器的 Firestore v9 onSnapshot

[英]Firestore v9 onSnapshot handling the listener

I have an issue with one part of Firestore.我对 Firestore 的一部分有疑问。 I'm trying to separate all of my data queries in a separate files, so from the front end I don't need to repeat myself with the queries.我试图将我所有的数据查询分离到一个单独的文件中,因此从前端我不需要重复查询。 So in the separate files I'm creating functions that take parameters and return the data.因此,在单独的文件中,我正在创建接受参数并返回数据的函数。 All good so far.到目前为止一切都很好。 When I use getDoc(doc(db, 'Projects', 'id) it work perfectly fine, it returns the section:当我使用 getDoc(doc(db, 'Projects', 'id) 它工作得很好,它返回部分:

const getSection = async (projectID, sectionID) => {
    const docRef = doc(db, `Projects/${projectID}/Sections`, `s${sectionID}`)
    const section = await getDoc(docRef)

    return section.data();
}

But my project requires to use onSnapshot to listen to changes in the database and change the frontend appropriately.但是我的项目需要使用 onSnapshot 来监听数据库中的变化并适当地更改前端。 Problem is I don't seem to be able to get a handle on the data coming from the onSnapshot listener.问题是我似乎无法处理来自 onSnapshot 侦听器的数据。 I can log the data, but I can't store it in a variable that I can return.我可以记录数据,但不能将其存储在可以返回的变量中。 Example:例子:

const getSection = async (projectID, sectionID) => {
    const docRef = doc(db, `Projects/${projectID}/Sections`, `s${sectionID}`)
    
    let section = {}
    const unsub = onSnapshot(docRef, (doc) => {
        console.log(doc.data());   // This log the exact data that I want

        section = doc.data();
    })
    console.log(section)    // This return undefined

    return section;         // So this returns absolutely nothing
}

**** EDIT **** **** 编辑 ****

I'm able to do it when I use onSnapshot to access collections:当我使用 onSnapshot 访问 collections 时,我能够做到这一点:

    const getAllSections = async (sectionID, projectID) => {
        let sections = [];
        const sectionCollectionRef = collection(db, `Projects/${projectID}/Sections`)

        onSnapshot(sectionCollectionRef, (snap) => {
            snap.forEach((section) => {
                sections.push(section.data())
            })
        })
        console.log(sections) // This works perfectly and every time something changes it refreshes the frontend
    
    return sections;
}

So this is what I'm trying to do with getting info from a document, instead of a collection所以这就是我试图从文档而不是集合中获取信息的方法

Since you say you're using react, this should be done in a useEffect, and you should set state each time the new data arrives.既然你说你正在使用反应,这应该在 useEffect 中完成,并且每次新数据到达时你都应该设置 state。

const SomeComponent = () => {
  const [section, setSection] = useState();
  useEffect(() => {
    const docRef = doc(db, `Projects/${projectID}/Sections`, `s${sectionID}`)
    
    const unsub = onSnapshot(docRef, (doc) => {
      setSection(doc.data();
    })
    return unsub
  }, [sectionId]);

  // ... rest of the component which uses `section`
}

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

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