繁体   English   中英

在currentSession()上使用.then时,aws-amplify仍返回promise

[英]aws-amplify still returning a promise when using .then on currentSession()

在React / Redux中,我试图使用Redux Connect将AWS放大会话数据映射到道具。

我的问题是,即使当我使用.then时,我总是会得到一个承诺,结果看起来像这样:

Promise {<resolved>: CognitoUserSession}
  __proto__: Promise
  [[PromiseStatus]]:"resolved"
  [[PromiseValue]]:CognitoUserSession

和代码如下。

import { connect } from 'react-redux';
import LoginForm from './LoginForm';
import {Auth} from 'aws-amplify';

export const mapStateToProps = (state) => { return {}; };
export const mapActionsToProps = () => {
  const session = Auth.currentSession().then((res) => { return res });
  return {
    idToken: session,
  };
};

export default connect(mapStateToProps, mapActionsToProps)(LoginForm);

我尝试使用JSON.stringify,Promise.resolve()等进行解析...我总是能得到应有的回报。

如果我在redux连接器中运行它,还是直接在页面上运行它,也尝试了两次,这似乎都没有关系,然后认为这可能是一个包含在promise中的promise,但事实并非如此。

mapActionsToProps不是执行异步获取的最佳位置。 我建议要么在componentWillMount函数中获取会话,然后将会话存储在组件状态中,要么创建一个redux thunk操作,以将会话获取并存储在redux状态树中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM