javascript - 使用firebase在react js中映射
问题描述
我有两个集合,它们是“主题”和“用户”,我想将它们映射到同一个箭头函数中,以便能够在一个箭头函数中检索 user.email、topic.topic 和 topic.date。我想在一个箭头函数中映射到来自 firebase 的不同集合。这是我的代码:
import React, { Component } from "react";
import { Link } from "react-router-dom";
import { compose } from "redux";
import { connect } from "react-redux";
import { firestoreConnect } from "react-redux-firebase";
import PropTypes from "prop-types";
import Spinner from "../layout/Spinner";
class Topics extends Component {
state = {};
render() {
const { topics, users } = this.props;
if ((topics, users)) {
return (
<div>
<div className="row">
<div className="col-md-6 text-center text-info">
<h2>
<i className="fas fa-comments" /> Micro Bloggin
</h2>
</div>
</div>
<table className="table table-striped">
<thead className="thead-inverse">
<tr>
<th>User</th>
<th>Topic</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<--!here I want to introduce user.email instead of
//topic.Username -->
{topics.map(topic => (
<tr>
<td>{topic.userName}</td>
<td>{topic.topic}</td>
<td>{topic.date}</td>
</tr>
))}
</tbody>
</table>
</div>
);
} else {
return <Spinner />;
}
}
}
Topics.propTypes = {
firestore: PropTypes.object.isRequired,
topics: PropTypes.array,
users: PropTypes.array
};
<--!here I call the two collections -->
export default compose(
firestoreConnect([{ collection: "topics" }, { collection: "users"
}]),
connect((state, props) => ({
topics: state.firestore.ordered.topics,
users: state.firestore.ordered.users
}))
)(Topics);
解决方案
如果该userName
值在集合中也可用,users
也许你可以尝试这样的事情:
{topics.map(topic => (
<tr>
<td>
{users.find(user=>user.userName === topic.userName).email}
</td>
<td>{topic.topic}</td>
<td>{topic.date}</td>
</tr>
))}
推荐阅读
- postgresql - 可以在 postgres 中对默认值使用表达式吗?
- python-xarray - Curvillinear Dataset.swap_dims 或 ncks --rgr 推断坐标变量规范选项
- python - Pandas:根据股票价格获取目标和损失指标,达到一定的比率并动态改变基本价格
- ios - iOS Voice Over 焦点位于 UIWindow 下方
- javascript - 数据表删除导出到 pdf 和 excel 的列
- javascript - 显示在无和块之间切换时的flickity.js画廊重叠文本
- java - 如何在 FreeTextMarker 中绑定 HTML 字符串?
- python - 我们如何遍历坐标项目,将每个项目加载到 Google 地图页面中,并从每个页面中获取一个元素?
- javascript - 使用js在url中添加或更改参数
- jenkins - Jenkins spock:模拟 http-request-plugin 的 httpRequest