简体   繁体   English

如何访问 redux 传奇中的 promise 数据

[英]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.

相关问题 Redux Saga:等待承诺 - Redux Saga: Await Promise 在 Redux-saga 中,如何从对 Promise 的 yield 调用中获得结果? - In Redux-saga, how to get a result from a yield call to a Promise? 如何在进程的不同阶段将数据临时存储在 redux 或 redux 传奇中 - how to temporarily store data in redux or redux saga on different phases of a process redux saga操作如何延迟存储数据更新? - How delay redux saga action to store data update? 将基于promise的React应用程序转换为redux-saga:如何设置React组件变量? - Converting promise-based React app to redux-saga: how do I set React component variables? 如何将错误从Promise返回到redux saga catch块? 现在得到未定义的错误 - How to return the error from a promise to a redux saga catch block? Now get the error undefined redux-saga 中的“调用”助手(效果)函数如何返回解析值而不是承诺? - How "call" helper(effect) function in redux-saga return the resolved value instead a promise? 如何在redux中添加一个promise,根据Data执行 - How to add a promise in the redux and execute according to Data Redux-Saga无法在Promise中使用生成器功能吗? - Redux-saga cannot use generator function inside promise? 使用 React/Redux saga 将数据从组件传递到 action 和 saga - Pass data from component to action and saga using React/Redux saga
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM