如何使firestoreConnect指向React-Redux-Firebase中的嵌套集合?

时间:2018-12-29 09:39:53

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

下面一行将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',
  }
])

但是这种尝试失败了。

1 个答案:

答案 0 :(得分:5)

可以通过将多个收集/文档设置传递给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的作者