我使用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函数中,但调用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}

最佳答案

checkUsername函数中,您需要将调用返回给fetch()

09-18 07:07