0

Hello I would like to know what is wrong with my code:
Even though my ajax request works, the payload object is always undefined.

If I understand sagas correctly the fetchWord function should wait for the fetchWordRequest promise to be resolved, right ?

export function* fetchWordSaga(action) {
   try {
      const { payload } = yield call(fetchWordRequest, action.payload);
      if (typeof payload !== "undefined") {
         yield put({
            type: types.FETCH_WORD_SUCCESS,
            payload: digestResponse(payload)
         })
      } else {
         throw new Error("payload is undefined");
      }
   }
   catch(error) {
      yield put({
         type: types.FETCH_WORD_ERROR,
         error: error.message
      })
   }
}

export function fetchWordRequest({word, params}) {
   let { lang, filters } = params;
   let url = `https://.../${lang}/${word}`;
   return axiosConfig.get(url)
       .then(response => {
          return response;
       })
       .catch(error => {
          throw error
       })
}
  • 2
    would have to know what `response` from `fetchWordRequest` looks like. Is there a `payload` property in the response json? Did the response match your expectations if you inspect it in your dev tools? – Damon Sep 16 '19 at 15:10
  • you are right, `fetchWordRequest` returns `{}`. I will look into it – Lorenzo Rivosecchi Sep 16 '19 at 15:16
  • You should find what you're looking for on the `.data` property, not the `.payload` property. `const { data } = yield call(fetchWordRequest, action.payload);` – Nicholas Tower Sep 16 '19 at 18:47

0 Answers0