返回未定义而不是已解决的承诺值的收益调用

时间:2019-01-07 09:47:24

标签: redux redux-saga

我是Redux-Saga的新手。以下是我的api调用的代码-

const registerUserRequest = (email, password, confirm_password, country_code, phone_number) =>
{
fetch(URLs.baseUrl + URLs.signUpUrl(email, password, confirm_password, country_code, phone_number), {
    method: 'POST',
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache, no-store, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': 0
    },
})
}

function* registerUserToService(action)
{
try{
    const response = yield call(registerUserRequest, action.email, action.password, action.confirm_password, action.country_code, action.phone_number);
    const result = yield response.json();   
    if(result.error)
    {
        yield put({ type: REGISTER_USER_ERROR, error: result.error})
    }
    else
    {
        yield put({ type: REGISTER_USER_RESULT, result});
    } 
}
catch(e)
{
    console.log('registerUserException', e.message);
    yield put({ type: REGISTER_USER_ERROR, error: e.message})
}
};

但是我得到的响应是未定义的,而不是已解决的承诺值,并且从响应中获取结果会生成异常-无法读取未定义的值json。 希望能对您有所帮助。

2 个答案:

答案 0 :(得分:1)

函数registerUserRequest中不返回任何内容。如果您想获得承诺,请返回fetch

const registerUserRequest = (email, password, confirm_password, country_code, phone_number) =>
{
return fetch(URLs.baseUrl + URLs.signUpUrl(email, password, confirm_password, country_code, phone_number), {
    method: 'POST',
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache, no-store, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': 0
    },
})
}

答案 1 :(得分:0)

获取不需要{}。删除{}周围的抓取。这样做吧。

const registerUserRequest = (email, password, confirm_password, country_code, phone_number) =>
fetch(URLs.baseUrl + URLs.signUpUrl(email, password, confirm_password, country_code, phone_number), {
    method: 'POST',
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache, no-store, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': 0
    },
})