如何使用react-redux-firebase从Firestore获取子集合

时间:2019-05-19 15:01:19

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

我无法从Firestore数据中获取子集合。

我遵循了文档here。并在此上也看了许多其他类似的问题。

我似乎无法获取想要获取的数据数组。

我的代码:

import "../../styles/projects/ProjectDashboard.css";
import ProjectList from "./ProjectList";
import { connect } from "react-redux";
import { firestoreConnect } from "react-redux-firebase";
import { compose } from "redux";
import { Redirect } from "react-router-dom";

export class ProjectDashboard extends Component {
  render() {
    const { projects, auth } = this.props;
    if (!auth.uid) return <Redirect to="/sigin" />;
    return (
      <div className="container">
        <h2>Projects</h2>
        <ProjectList projects={projects} />
      </div>
    );
  }
}

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

export default compose(
  connect(mapStateToProps),
  firestoreConnect(props => {
    return [
      {
        collection: "users",
        doc: props.auth.uid,
        subcollection: [{ collection: "projects" }]
      }
    ];
  })
)(ProjectDashboard);

这就是我现在从控制台获得的内容:

firestore:
|-composite: undefined
|-data: {users: {…}}
|-errors: {byQuery: {…}, allIds: Array(0)}
|-listeners: {byId: {…}, allIds: Array(1)}
|-ordered:
|--users: Array(1)
|---0: {id: "o06VEyRbGDcURTmKal3gDEDZQO73", firstName: "testing", lastName: "you", userId: "o06VEyRbGDcURTmKal3gDEDZQO73"}
|---length: 1
|---__proto__: Array(0)
|--__proto__: Object
|-queries: {}
|-status: {requesting: {…}, requested: {…}, timestamps: {…}}
|-__proto__: Object

我当时以为我的代码会改变这部分:

|-ordered:
|--users: Array(1)
|---0: {id: "o06VEyRbGDcURTmKal3gDEDZQO73", firstName: "testing", lastName: "you", userId: "o06VEyRbGDcURTmKal3gDEDZQO73"}

类似这样:

|-ordered:
|--projects: Array(3)
|---0: {id: "o06VEyRbGDcURTmKal3gDEDZQO73", firstName: "testing", lastName: "you", userId: "o06VEyRbGDcURTmKal3gDEDZQO73"}
|---1: {id: "o06VEyRbGDcURTmKal3gDEDZQO73", firstName: "testing", lastName: "you", userId: "o06VEyRbGDcURTmKal3gDEDZQO73"}
|---2: {id: "o06VEyRbGDcURTmKal3gDEDZQO73", firstName: "testing", lastName: "you", userId: "o06VEyRbGDcURTmKal3gDEDZQO73"}

所以我可以通过键获取数组。但是现在我迷失了如何将其更改为子集合密钥。

1 个答案:

答案 0 :(得分:0)

我意识到应该是“子集合”而不是“子集合”