reactjs - 如何从firestore中仅获取特定的数据字段
问题描述
我需要在我的数据库中获取加油站的所有品牌名称并在 React 组件中显示这些名称,但我不确定我应该使用什么查询以及我应该如何存储这些数据。
我已经尝试从 Firestore 获取所需的信息并将其存储到 redux 中,但它不起作用。我应该使用单独的减速器吗?
export const setStations = stations => ({
type: actionTypes.SET_STATIONS,
stations
});
export const setStationsNames = stationsNames => ({
type: actionTypes.SET_STATIONS_NAMES,
stationsNames
});
export const startSetStations = () => {
return dispatch => {
return db
.collection('stations')
.get()
.then(snapshot => {
const stations = [];
const stationsNames = [];
snapshot.docs.forEach(doc => {
stations.push(doc.data());
stationsNames.push(doc.data().brand);
});
dispatch(setStationsNames(stationsNames));
dispatch(setStations(stations));
});
};
};
{props.stations.map((station, i) => {
return (
<li key={i} className="station">
<h3 className="station__text">{station.brand}</h3>
</li>
);
})}
减速器
export default (state = stationsReducerDefaultState, action) => {
switch (action.type) {
case actionTypes.SET_STATIONS:
return action.stations;
default:
return state;
}
};
解决方案
推荐阅读
- bash - ssh -T git@github.com 有问题
- data-visualization - Tableau:在具有两个双轴的图表中隐藏值
- python - 根据第二个字典中的值过滤字典
- boolean - implementing two commands in one boolean case
- postgresql - postgreSQL 错误创建带有引用的表
- javascript - Electron 从根目录获取 React 资源
- c++ - 字段访问 - C++ 中的多态和非多态类型
- kendo-ui - Kendo Dropdownlist 在过滤多列时抛出错误
- c# - 为什么我的 Web API 项目中出现“HttpException”缺失命名空间错误
- ssrs-2008 - SSRS - 如何在柱形图上显示前 N 个