首页 > 解决方案 > 递归获取用户列表 [Firebase + Saga]

问题描述

由于 Firebase 的 Firestore 在每个查询中只允许 10 个项目。我需要以 10 个用户为一组递归查询用户。

这是我从撰写 Sagas 中引用的内容,但users返回了undefined. 我做错了什么?或者是否有更好(更有效)的方式从 Firestore 查询用户列表?注意:我必须使用 Redux-Saga。

export function* watchGetListUsersByUids() {
  yield takeEvery(USERS_GET_USERS_BY_UIDS, getListUsersByUids);
}

const getListUsersByUidsAsync = async (list_uid, set) => {
  var get_uids = list_uid.slice(set * 10, (set + 1) * 10);
  console.log("get_uids", get_uids);
  await firestore
    .collection("users")
    .where("uid", "in", get_uids)
    .get()
    .then((data) => data.docs.map((doc) => doc.data()))
    .catch((error) => error);
};

function* getListUsersByUids({ payload }) {
  const { uids } = payload;

  var numSet = parseInt(uids.length / 10);

  var apiCalls = [];
  for (var i = 0; i <= numSet; i++) {
    apiCalls.push(call(getListUsersByUidsAsync, uids, i));
  }

  if (apiCalls.length) {
    try {
      const users = yield all(apiCalls);
      console.log(users); // <-- [undefined, undefined, ...]
    } catch (error) {
      console.log(error);
    }
  }
}

标签: firebasegoogle-cloud-firestoreredux-saga

解决方案


推荐阅读