【发布时间】:2018-07-30 08:43:06
【问题描述】:
我在 React 中有一个名为 ShowArticle 的组件,我试图通过一个名为 slug 的字符串字段在我的 articles 集合中查询 Firestore 中的单个文档,以便我可以将我的 URL 格式化为 //myhost.com/article/show/:slug其中 slug 是我的 Firestore 中匹配的唯一字符串。
作为我的路线,我有:
<Route exact path="/article/show/:slug" component={ShowArticle} />
所以我在ShowArticle 组件内部使用const { slug } = props.match.params; 正确获取了:slug 参数。
当我在 Firestore 中查询数据库中存在的 slug 时,我没有从 Firestore 收到任何数据。
如何通过 SEF URL 的唯一字符串值检索文章文档?
我的 ShowArticle 组件如下:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
import { firestoreConnect } from 'react-redux-firebase';
import { PropTypes } from 'prop-types';
import Article from "./Article";
class ShowArticle extends Component {
render() {
const { article } = this.props;
if (article) {
return (
<div>
<Article
key={article.id}
title={article.title}
date={article.date}
body={article.body}
/>
</div>
);
} else {
return (
<div>Loading...</div>
)
}
}
}
ShowArticle.propTypes = {
firestore: PropTypes.object.isRequired
};
export default compose(
firestoreConnect(props => {
const { slug } = props.match.params;
console.log(slug);
return [
{collection: 'articles', storeAs: 'article', doc: slug }
]
}),
connect(({ firestore: { ordered } }, props) => ({
article: ordered.article && ordered.article[0]
}))
)(ShowArticle);
【问题讨论】:
标签: javascript reactjs firebase google-cloud-firestore