首页 > 解决方案 > AWS 放大,等待 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 我得到这个:

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}

所以,我myAttribute在调用函数时得到了一些奇怪的输出,但来自 Cognito 的真实数据稍后会出现userInfo

我怎样才能得到真实的数据myAttribute呢?

标签: javascriptaws-amplify

解决方案


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

换线 const myAttribute = getMyAttribute(); 可以 const myAttribute = async getMyAttribute();提高产量吗?


推荐阅读