首页 > 解决方案 > 如何从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;
  }
};

我的 Firebase 数据库

标签: reactjsreact-reduxgoogle-cloud-firestore

解决方案


推荐阅读