2016-09-20 2 views
2

redux-saga를 사용하고 API 호출을 수행하는 생성자 checkUsername을 생성했습니다. 나는 const username이 API의 응답과 같지만 undefined을 얻었습니다. API res 전화 내 checkUsername 기능에 비록 Redux-saga가 API의 응답을 기다리지 않습니다.

function* checkUsername(action) { 
    try { 
    const username = yield call(Api.checkUsername, action.username); 
    yield put({type: actions.USERNAME_CHECK_SUCCEEDED, username}); 
    } catch (e) { 
    yield put({type: actions.USERNAME_CHECK_FAILED, message: e.message}); 
    } 
} 

은 동일 {"isExist": false}입니다 :

checkUsername(username) { 
    fetch(url, { 
    'GET', 
    headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json', 
    } 
    }).then(response => response.json()) 
     .then(res => res) 
     .catch(error => console.error(error)); 
}, 

const username{"isExist": false} 동일하지 않은 이유는 무엇입니까?

답변

2

checkUsername 함수에서 fetch()으로 전화해야합니다.

+0

정말 고마워요! – rel1x

+0

내 날을 저장했습니다! 감사.! –

관련 문제