Redux-Saga的yield调用返回未定义

6
给定以下代码,我的yield call返回undefined。这是为什么呢?我认为可能与我的请求函数有关。不确定为什么。
在我的saga文件中,这一行返回了undefined:const result = yield call(request, Routes.insightTotals); 我已经验证了请求正在正确触发并返回。可能是我的promise链导致这个结果是undefined吗? HTTP请求函数
function request(url, options) {
  return fetch(url, options)
    .then(parseJSON)
    .then(checkStatus);
}

function parseJSON(response) {
  if (response.url === 'https://myurl.com') {
    // This returns as a GZIP body so need to convert to text then parse as JSON
    return response.text()
      .then((res) => {
        const parsed = JSON.parse(res);
        console.log(parsed)
        return parsed;
      }).catch((err) => {
        throw new Error(err);
      });
  }
  return response.json();
}

Saga file

export function* getInsightTotalsRequestHandler() {
  yield takeEvery(actions.GET_INSIGHT_TOTALS, function* getInsightTotalsRequest() {
    try {
      const result = yield call(request, Routes.insightTotals);
      console.log(result); // THIS IS UNDEFINED

      yield put({
        type: actions.GET_INSIGHT_TOTALS_RETURN,
        value: { result },
      });
    } catch (error) {
      yield put({
        type: actions.GET_INSIGHT_TOTALS_RETURN,
        value: { error: error.message ? error.message : '' },
      });
    }
  });
}

export default function* mySaga() {
  yield all([
    fork(other1RequestHandler),
    fork(other2RequestHandler),
    fork(getInsightTotalsRequestHandler),
  ]);
}

3
你是否解决了这个问题?我也遇到了同样的问题,使用了你相同的模板React-Boilerplate。我也从调用中得到了undefined。但不同的是你说请求已经发送了,而我的没有发送。 - jaysonder
@jaysonder 我了解到我们需要从react-saga/effects中导出调用。 - Alexey Nikonov
你解决了吗?如果是,请告诉我们你的方法。 - Alekhya Satya
1个回答

1

是的,它会。 call 将返回解析后的 promise 值。我最好的猜测是 checkStatus 没有返回值(你没有展示它)。


新手注意啦(就像我一样),你需要import { call } from 'redux-saga/effects',否则会出现call is not defined的错误。 - Alexey Nikonov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接