Redux-Saga yield call returning back as undefined
Asked Answered
B

1

6

Given the following code, I am getting an undefined result back from my yield call. Why is this happening? I am thinking its something with my request function. Not sure why.

In my saga file, this line is coming back undefined: const result = yield call(request, Routes.insightTotals);

I have verified the request is firing off and returning correctly. Could it be my promise chain that is causing this to be undefined?

HTTP Request functions

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),
  ]);
}
Bluetongue answered 5/2, 2018 at 6:44 Comment(3)
Did you ever solve this issue? I'm actually experiencing this same issue, using your same boilerplate, React-Boilerplate. I also get undefined from that call. But the difference is that you say the request is firing off, but mine doesnt.Hopefully
@Hopefully i learned we need to export call from react-saga/effectsMarya
Were you able to solve it?If yes,please let us know the approach.Great
Z
1

Yes, it is. call will return the resolved promise value. My best guess is that checkStatus does not return a value (You're not showing it).

Zuleika answered 5/2, 2018 at 19:6 Comment(1)
notice for newbies (as im :) ) you need to import { call } from 'redux-saga/effects' otherwise call is not definedMarya

© 2022 - 2024 — McMap. All rights reserved.