首页 > 解决方案 > 如何在firestore(firebase)过滤数据并获取过滤后的数据?如何在数据库集合中使用“等于”?

问题描述

export const getSprintByProjectId = (key) => async (dispatch) => {
  console.log(key);
  try {
    dispatch(loaderOn());
    const result = await db
      .collection("sprints")
      .orderByChild("projectId")
      .equalTo(key);
  } catch (error) {
    dispatch(errorOn());
  } finally {
    dispatch(loaderOff());
  }
};

这是我的火库的结构在此处输入图像描述

试图通过 ID 获取数据

标签: javascriptreactjsfirebasegoogle-cloud-firestore

解决方案


您的 JS 代码看起来像是在尝试在 Firestore 上使用实时数据库查询方法。我希望它看起来更像:

export const getSprintByProjectId = (key) => async (dispatch) => {
  console.log(key);
  try {
    dispatch(loaderOn());
    const result = await db
      .collection("sprints")
      .where("projectId", "==", key)
      .get();
    console.log(result.docs.map(d => d.data());
  } catch (error) {
    dispatch(errorOn());
  } finally {
    dispatch(loaderOff());
  }
};

推荐阅读