5

我使用 redux-saga 并创建了一个checkUsername执行 API 调用的生成器。我虽然这const username将等于来自 API 的响应,但我有undefined.

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函数中,调用 APIres是相等的{"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}

4

1 回答 1

5

在您的checkUsername函数中,您需要将调用返回到fetch().

于 2016-09-20T20:30:27.523 回答