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

Posted

技术标签:

【中文标题】如何使用 react-redux-firebase 从 firestore 获取子集合【英文标题】:How to get subcollection from firestore using react-redux-firebase 【发布时间】:2019-10-06 02:55:17 【问题描述】:

我无法从我的 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"

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

【问题讨论】:

请参考同一作者的 redux-firestore。阅读此链接的子集合部分:github.com/prescottprue/redux-firestore 确保将 storeAs 用于子集合。 【参考方案1】:

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

【讨论】:

以上是关于如何使用 react-redux-firebase 从 firestore 获取子集合的主要内容,如果未能解决你的问题,请参考以下文章

如何纠正 Redux 商店中的 React-Redux-Firebase 错误

如何使用来自 react-redux-firebase 的 redux thunk getFireBase 和 getFirestore 将数据添加到 firebase doc

如何迁移到 react-redux-firebase v3

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

`react-redux-firebase` 解决了啥问题?

使用 Firestore 通过“react-redux-firebase”存储配置文件数据?