问题描述
我正在尝试使用 redux saga 从服务器获取用户数据。我能够成功获取数据并且可以在控制台中看到它,但它在 [[PromiseResult]] 中。我无法找到有关如何访问这些数据的任何信息,或者我是否在我的传奇中做错了什么导致数据像这样到达。
传奇
function* login() {
try {
const response = yield call(fetch(`${API_URL}/api/current_user`,{
method: 'GET',credentials: 'include',}));
const responseBody = yield response.json();
yield put(loginUserSuccess(responseBody));
} catch (error) {
let message;
switch (error.status) {
case 500:
message = 'Internal Server Error';
break;
case 401:
message = 'Invalid credentials';
break;
default:
message = error;
}
yield put(loginUserFailed(message));
// setSession(null);
}
}
有效负载我可以控制台.log
Promise {<fulfilled>: {…}}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Object
{
credits: 0
date: "2020-12-21T22:23:43.461Z"
email: "test@gmail.com"
password: "$2a$10$W.GOdcdyfphcazW.9flFTeoQ4s/3khfZOv2dkyJ2Bg/gl0pIZRtHu"
plan: 1
verified: false
__v: 0
_id: "5fe1206f4a30e03194bfdf0b"
}
__proto__: Object
解决方法
这不是 call works 第一个参数是一个函数,第二个参数是您想要传递给该函数的参数数组。以下应该有效:
const response = yield call(fetch,[
`${API_URL}/api/current_user`,{
method: 'GET',credentials: 'include',},]);
const responseBody = yield call(() => response.json());