程序问答   发布时间:2022-05-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AWS Amplify,等待 Auth.currentUserInfo大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决AWS Amplify,等待 Auth.currentUserInfo?

开发过程中遇到AWS Amplify,等待 Auth.currentUserInfo的问题如何解决?下面主要结合日常开发的经验,给出你关于AWS Amplify,等待 Auth.currentUserInfo的解决方法建议,希望对你解决AWS Amplify,等待 Auth.currentUserInfo有所启发或帮助;

我需要从 Cognito 获取一些自定义属性,并对这些信息进行处理。

我有这个功能:

const getMyAttribute = async (): String => {
  const userInfo = await Auth.currentUserInfo();
  console.log('userInfo',userInfo);
  return userInfo.attributes['custom:myAttribute']
};

我在一些 React 组件中调用它:

const myAttribute = getMyAttribute();
console.log('myAttribute:',myAttributE); // Here is weird output
// Here I need to do something with `myAttribute`

在 console.log 中我得到这个:

@H_448_7@myAttribute:  {"_40": 0,"_55": null,"_65": 0,"_72": null}

userInfo: {"attributes": {"custom:myAttribute": "6","email": "testpending4@test.cl","sub": "etc..."},... rest of user info data from Cognito}

因此,在调用该函数时,我在 @H_448_7@myAttribute 中得到了一些奇怪的输出,但是来自 Cognito 的真实数据稍后会在 userInfo

我怎样才能获得关于 @H_448_7@myAttribute 的真实数据?

解决方法

在您对 getMyAttribute() 的呼叫站点上,您似乎缺少一个 async 关键字。

是否改变线路 const myAttribute = getMyAttribute();const myAttribute = async getMyAttribute(); 提高输出?

,

从您的代码来看,getMyAttribute 是 promise 方法。这就是为什么你在调用它时会得到异步进程。要正确调用它,您必须使用 async - await 并且您已经完成了其他问题。其他方式,你试过这个吗?

let myAttribute;
getMyAttribute().then(attribute => {
    myAttribute = attribute;
})

大佬总结

以上是大佬教程为你收集整理的AWS Amplify,等待 Auth.currentUserInfo全部内容,希望文章能够帮你解决AWS Amplify,等待 Auth.currentUserInfo所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:Amplify等待