Redux Sagas递归不起作用

Redux Sagas递归不起作用,redux,redux-saga,Redux,Redux Saga,我想按项目id查找项目及其子项目,我编写了以下代码,但fetchSubItems()始终不工作,并引发异常“TypeError:无法读取未定义的属性“root”,有人可以帮助我吗 export function *fetchItem(api, id){ const item = yield call (api.getItem, id) yield put(Actions.addItem(item)) yield call(fetchSubItems, item) yield pu

我想按项目id查找项目及其子项目,我编写了以下代码,但fetchSubItems()始终不工作,并引发异常“TypeError:无法读取未定义的属性“root”,有人可以帮助我吗

export function *fetchItem(api, id){
  const item = yield call (api.getItem, id)
  yield put(Actions.addItem(item))
  yield call(fetchSubItems, item)
  yield put(Actions.success())
}

export function *fetchSubItems(api, item){
  if(item.children){
    const children = yield item.children.map((id)=>{
      return call(api.getItem, id)
    })
    yield put(Actions.addItems(children))

    // the following lines throws 'TypeError: Cannot read property 'root' of undefined'
    yield children.map((child)=>{
      call(fetchSubItems, api, child)
    })
  }
}

上次调用中似乎缺少
return
语句。工作示例:

import Promise from 'bluebird';
import { delay } from 'redux-saga';
import { call } from 'redux-saga/effects';

import {
  reducer
} from '../reducers/counter';
import { logger } from '../utils';

const name = '19/Tree_Traversal';
const log = logger(name);

const delayTime = 10;

const tree = {
  1: {children: [2, 3]},
  2: {children: [4, 5, 6]},
  3: {children: []},
  4: {children: []},
  5: {children: []},
  6: {children: [7]},
  7: {children: []}
};

const api = {
  getItem(id) {
    log(`getItem(${id})`);
    return delay(delayTime, tree[id]);
  }
};

export function *fetchItem(/*api, */id = 1) {
  const item = yield call(api.getItem, id);
  // yield put(Actions.addItem(item))
  yield call(fetchSubItems, /*api, */item);
  // yield put(Actions.success())
}

export function *fetchSubItems(/*api, */item) {
  if (item.children) {
    const children = yield item.children.map((id) => {
      return call(api.getItem, id);
    });
    // yield put(Actions.addItems(children))

    yield children.map((child) => {
      return call(fetchSubItems, child); // <=== added `return`
    });
  }
}

export default  {
  name,
  saga: fetchItem,
  reducer: reducer,
  useThunk: !true,
  execute(store) {
    return Promise.delay(8 * delayTime)
    .then(() => this);
  }
};
   00000000: [counter reducer] action Object {type: "@@redux/INIT"}
   00000003: [Runner] ---------- running example 19/Tree_Traversal
   00000004: [Runner] store initial state 0
   00000008: [19/Tree_Traversal] getItem(1)
 * 00000060: [19/Tree_Traversal] getItem(2)
   00000061: [19/Tree_Traversal] getItem(3)
 * 00000074: [19/Tree_Traversal] getItem(4)
   00000074: [19/Tree_Traversal] getItem(5)
   00000075: [19/Tree_Traversal] getItem(6)
 * 00000088: [19/Tree_Traversal] getItem(7)
   00000091: [Runner] store final state 0
   00000092: [Runner] ---------- example 19/Tree_Traversal is done