2016-09-20 49 views
2

響應我用終極版,傳奇和我創建了一個發電機checkUsername其執行API調用。我雖然const username將等於來自API的響應,但我得到undefined終極版,傳奇不會等待來自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}); 
    } 
} 

雖然我checkUsername功能,調用API res等於{"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

保存了我的一天。謝謝。! –