如何在数组映射的redux saga中进行并行请求循环到Firebase?

时间:2019-10-02 19:36:32

标签: javascript firebase redux redux-saga

所以我有一组用户ID。我想从集合中获取有关它们的数据。这是传奇的故事:

 const fetchedMembers = yield all(tasksObject[task].members.map( member => {
//member has id of user!
              const user = call(() => db.collection('users').doc(member).get())
              const userData = user.data()
              return {
                uid: member,
                ...userData
              }
            }))

但是我的代码有误。如何正确拼写?

1 个答案:

答案 0 :(得分:0)

传递给yield all的数组应该是一个效果数组。如果您稍微进行重构,则可以执行以下操作:

// define this helper generator
function* fetchMember(member) {
  // note the addition of the `yield` here before `call`
  const user = yield call(() => db.collection('users').doc(member).get())
  const userData = user.data()
  return {
    uid: member,
    ...userData
  }
}

...

 const fetchedMembers = yield all(tasksObject[task].members.map(
   // the array being passed to `yield all` is now an array of `call` effects
   member => call(fetchMember, member)
 ));