Javascript 如何从Firestore加载具有相关字段的多个集合?

Javascript 如何从Firestore加载具有相关字段的多个集合?,javascript,google-cloud-firestore,react-redux,Javascript,Google Cloud Firestore,React Redux,假设我有两个模型 职位 作者 作者如下: { authorId: 'ghr4334t', fullName: 'This is a post!' Nickname: 'Avola } { postId: '12fdc24', authorId: 'ghr4334t', content: 'This is a post!' } getPost(postId).then(post=> { getAuthor(listing.uid).t

假设我有两个模型

  • 职位
  • 作者
  • 作者如下:

    {
        authorId: 'ghr4334t',
        fullName: 'This is a post!'
        Nickname: 'Avola
    }
    
    {
        postId: '12fdc24',
        authorId: 'ghr4334t',
        content: 'This is a post!'
    }
    
    getPost(postId).then(post=> {
       getAuthor(listing.uid).then((document) => {
          getSomethingElse(listing.uid).then((document) => {
             getAnother(listing.uid).then((document) => {
                 // finally update state with everything.
             })
          })
       })
    })
    
    这篇文章如下所示,并将引用以下作者:

    {
        authorId: 'ghr4334t',
        fullName: 'This is a post!'
        Nickname: 'Avola
    }
    
    {
        postId: '12fdc24',
        authorId: 'ghr4334t',
        content: 'This is a post!'
    }
    
    getPost(postId).then(post=> {
       getAuthor(listing.uid).then((document) => {
          getSomethingElse(listing.uid).then((document) => {
             getAnother(listing.uid).then((document) => {
                 // finally update state with everything.
             })
          })
       })
    })
    
    当前,当用户单击帖子时,为了显示所有相关信息,我加载数据如下:

    getPost(postId)。然后(post=>{
    getAuthor(listing.uid).then((document)=>{
    //更新状态,使我拥有post对象和author对象。
    })
    })
    
    因此,在上面,我加载帖子,然后加载作者。加载完这两个对象后,我最终可以构建一个自定义对象:

    const finalPost = {
        author: { ...this.state.authorData },
        post: { ...this.state.postData }
    }
    
    当然..如果我还有两个引用其他集合的字段,那么将有一组
    get
    调用。then()
    调用如下:

    {
        authorId: 'ghr4334t',
        fullName: 'This is a post!'
        Nickname: 'Avola
    }
    
    {
        postId: '12fdc24',
        authorId: 'ghr4334t',
        content: 'This is a post!'
    }
    
    getPost(postId).then(post=> {
       getAuthor(listing.uid).then((document) => {
          getSomethingElse(listing.uid).then((document) => {
             getAnother(listing.uid).then((document) => {
                 // finally update state with everything.
             })
          })
       })
    })
    

    是否有更好的方法将相关信息加载到一起,而不必堆叠
    。then()
    调用

    不幸的是,没有更好的方法可以直接通过查询实现您想要的。Firestore中的查询没有为您提供许多关于如何查询和返回数据的选项,主要是当您需要对其执行任何类型的
    JOIN
    以通过引用进行搜索时,这使得工作不太容易。我相信你现在所做的是你获得更多的最好选择

    您可以尝试的另一种选择是,在您的收藏
    Post
    中有一个子收藏
    Author
    。这样,您将只处理
    Post
    的引用,因为
    作者将位于每个特定
    Post
    的文档中。这样,查询将更加简单,如下所示。当然,这需要您修改数据库

    var messageRef = db.collection('Post').doc('Post1')
                    .collection('Author').doc('Author1');
    
    如果您仍然认为这还不够,我建议您在上提交一个功能请求,Google开发人员可以在那里检查是否可以实现一种获取数据的新方法


    如果信息澄清了您的疑问,请告诉我

    谢谢你的建议!如果我理解正确的话,我想我唯一的问题是拥有一个子集合的每一篇文章都会有一个作者子集合,这是重复的数据。因此,两篇文章将有相同的作者子集合。我想我得用Firestore给我的东西。我很惊讶Firestore的团队没有更快地实现类似的功能。干杯。嗨@Zer0当然,我明白你的意思。事实上,Firestore没有非常深入的查询功能,但重复数据通常是许多用户在Firestore中都有的,但我理解你的观点。请,考虑一下我的回答,如果你认为它对你有帮助。