Why not work redux-saga?

Hi! Everything seemed simple:

const getAccounts = function* getAccounts() {
 while (true) {
 const request = yield take(GET_ACCOUNTS);
 const userID = request.data.userID;
 try {
 response = yield call(AccountsService.getAccounts, userID); // then an asynchronous call
 yield put({ type: ACCOUNTS_RECEIVED, response });
 } catch (error) {
 return false;
}
}
}


Reducer

export const accounts = (state = INITIAL_STATE, action) => {
 switch (action.type) {
 case ACCOUNTS_RECEIVED:
 console.log(action); // here action.response undefined
 return { ...state accounts: action.response.Items[0].accounts };
 case ADD_ACCOUNT:
 return { ...state, accounts: [ ...state.accounts, action.newAccount ] };
default:
 return state;
}
};


Saga waits for an asynchronous function returns immediately dispatched ACCOUNTS_RECEIVED with an empty response. Why?

Thank you.
June 8th 19 at 16:29
1 answer
June 8th 19 at 16:31
Solution
You have an error here:
export const getAccounts = (userID) => {
 fetch(`${BASE_URL}/users/${userID}/accounts/`).then(resp => {
 resp.json().then(accounts => accounts.Items[0].accounts);
})
}

Do not return anything.

To fix this:
export const getAccounts = userID =>
 fetch(`${BASE_URL}/users/${userID}/accounts/`).then(resp => {
 resp.json().then(accounts => accounts.Items[0].accounts);
 });


And better use axios, instead of fetch

The essence of the Service is useless in this architecture, import and use the function directly:
response = yield call(getAccounts, userID);
here then, before the fetch return was not. - Demetrius78 commented on June 8th 19 at 16:34

Find more questions by tags ReactRedux