yield 调用返回 undefined 而不是 resolved promise 值
yield call returning undefined rather than resolved promise value
我是 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。
如果能帮助理解这一点,我们将不胜感激。
您的函数 registerUserRequest
中没有 return 任何内容。如果你想得到承诺,returnfetch
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
},
})
}
获取不需要{}。移除围绕 fetch 的 {}。这样做吧。
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
},
})
我是 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。 如果能帮助理解这一点,我们将不胜感激。
您的函数 registerUserRequest
中没有 return 任何内容。如果你想得到承诺,returnfetch
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
},
})
}
获取不需要{}。移除围绕 fetch 的 {}。这样做吧。
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
},
})