Reputation: 2441
In my React-Native app I write a user log in component which send username and hash to server, compare hash with hash in database and return a result. I implemented that using redux-saga
:
function* fetchUser(action) {
try {
const user = yield call(Api.fetchUser, action);
yield put({type: types.USER_FETCH_SUCCEEDED, user});
} catch (e) {
yield put({type: types.USER_FETCH_FAILED, message: e.message});
}
}
export function* watchFetchUser() {
yield* takeEvery(types.USER_FETCH_REQUESTED, fetchUser);
}
export default function* rootSaga() {
yield fork(watchFetchUser);
// ...
}
And I expect that const user
will contain response from API and after will run yield put({type: types.USER_FETCH_SUCCEEDED, user})
. But after yield call(Api.fetchUser, action)
user
is undefined
. But Api.fetchUser
returns right response.
And I can't find where is my mistake. Why result of const user = yield call(Api.fetchUser, action)
is undefined
when after Api.fetchUser
return correct response?
fetchUser(action) {
const url = `${apiUrls.LOGIN_URL}`;
fetch(url, 'POST', action.user)
.then(response => response.json())
.then(res => res)
}
Upvotes: 0
Views: 164
Reputation: 8678
You are not returning the promise from the fetchUser function. It should be
fetchUser(action) {
const url = `${apiUrls.LOGIN_URL}`;
return fetch(url, 'POST', action.user)
.then(response => response.json())
.then(res => res)
}
Upvotes: 1