[英]How to access promise data in redux saga
I am trying to grab user data from the server using redux saga.我正在尝试使用 redux 传奇从服务器获取用户数据。 I'm able to successfully get the data and I can see it in the console, but it is in [[PromiseResult]].我能够成功获取数据,并且可以在控制台中看到它,但它在 [[PromiseResult]] 中。 I am unable to find any info on how to access this data, or if I am doing something wrong in my saga causing the data to arrive like this.我找不到有关如何访问此数据的任何信息,或者我在我的传奇中做错了什么导致数据像这样到达。
saga传奇
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);
}
}
payload I can console.log有效载荷我可以 console.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
That is not how call works first argument is a function and second is an array of argument(s) you want to pass to that function.这不是调用的工作方式,第一个参数是 function,第二个是要传递给 function 的参数数组。 The following should work:以下应该有效:
const response = yield call(fetch, [
`${API_URL}/api/current_user`,
{
method: 'GET',
credentials: 'include',
},
]);
const responseBody = yield call(() => response.json());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.