【问题标题】:How to point firestoreConnect to a nested collection in React-Redux-Firebase?如何将 firestoreConnect 指向 React-Redux-Firebase 中的嵌套集合?
【发布时间】:2019-05-26 20:39:01
【问题描述】:

下面的行将firestoreConnect 指向我标记为projects 的集合。

 { collection: 'projects' }

当项目集合像这样直接位于根目录下时,它可以工作:

root/
  projects/

但是如果项目集合被一个本身在另一个集合中的文档引用怎么办,比如说,像这样:

root/
  users/
    alice/
      projects/

如何将firestoreConnect 指向projects 集合?

The documentation is silent on the matter.

Link to video
import React, { Component } from 'react'
import ProjectList from '../projects/ProjectList'
import Notifications from './Notifications'
import { connect } from 'react-redux'
import { firestoreConnect } from 'react-redux-firebase'
import { compose } from 'redux'
import { Redirect } from 'react-router-dom'

class Dashboard extends Component {...}

const mapStateToProps = (state) => {
  // console.log(state);
  return {
    projects: state.firestore.ordered.projects,
    auth: state.firebase.auth,
  }
}

export default compose(
  connect(mapStateToProps),
  firestoreConnect([
    { collection: 'projects' }, // <-- Question is about this line
  ])
)(Dashboard)

编辑:不成功的尝试

From what I can piece together from the comments here 答案似乎是:

firestoreConnect([
  { 
    collection : 'users',
    doc        : 'alice',
    collection : 'projects',
  }
])

但那次尝试失败了。

【问题讨论】:

  • 对于任何错误的沟通,我深表歉意,但您能找到解决此问题的方法吗?在遵循完全相同的教程时,我遇到了类似的问题。我的预期结构看起来像“集合:课程,文档:state.auth.uid,集合:讲座”,但没有成功。

标签: reactjs firebase react-redux google-cloud-firestore react-redux-firebase


【解决方案1】:

可以通过将多个集合/文档设置传递给subcollections 参数来完成,如下所示:

firestoreConnect(() => [
  {
    collection: 'states',
    doc: 'CA',
    subcollections: [
      { collection: 'cities', doc: 'SF' },
      { collection: 'zips' }
    ]
  }
])

这在firestoreConnect section of the react-redux-firebase docs 中有说明(因为它是特定于反应的 HOC)。可以传递给查询are documented in the README of redux-firestore的选项。

如果你在做嵌套的子集合,你可能会想要使用v1.*.* 版本,因为子集合以 redux 状态存储在侧顶级集合中。请注意,新版本有一个different API , as described in the v1.0.0 roadmap

披露:我是 redux-firestore 的作者

【讨论】:

  • +1。我的 $.02:鼓励人们在 SO 上发布他们的问题。通过提供示例,它可以很好地扩充您的文档。有了足够多的例子,全面的文档就变得不那么重要了。 Gitter 有利于建立社区。如果人们在这里发布他们的问题,那么知识库会更加持久,并且也更容易搜索和查找。这也将帮助新用户发现您的图书馆。建造那个图书馆的工作很棒!
  • 缺少第一个数组内对象的右括号。
  • 我想知道所有这些都记录在哪里。我猜它映射到一些firestore API,但我还没弄清楚是哪个。 @Scott 您在哪里找到制作上述解决方案所需的文档?
  • the react-redux-firebase docs 中介绍了 firestoreConnect 的使用(因为 redux-firestore 不是特定于反应的),如 the README of redux-firestore 中所引用。
  • 在哪里可以找到关于“doc”字段的解释?
猜你喜欢
  • 2021-02-13
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2012-12-08
相关资源
最近更新 更多