Javascript AWS放大,等待Auth.currentUserInfo
我需要从Cognito获得一些自定义属性,并对这些信息进行处理 我有这个功能:Javascript AWS放大,等待Auth.currentUserInfo,javascript,aws-amplify,Javascript,Aws Amplify,我需要从Cognito获得一些自定义属性,并对这些信息进行处理 我有这个功能: const getMyAttribute = async (): string => { const userInfo = await Auth.currentUserInfo(); console.log('userInfo', userInfo); return userInfo.attributes['custom:myAttribute'] }; 我在这里用一些React组件来调用它: c
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
上的真实数据呢?在您调用getMyAttribute()
的站点上,您似乎缺少一个async
关键字
换线吗
const myAttribute=getMyAttribute()代码>
到
const myAttribute=async getMyAttribute()代码>改进输出?从代码中,getMyAttribute是promise方法。这就是为什么调用它时会得到异步化进程。要正确地调用它,您必须使用async-await,并且您已经解决了其他问题。换句话说,你试过这个吗
let myAttribute;
getMyAttribute().then(attribute => {
myAttribute = attribute;
})
评论。如果我使用wait调用getMyAttribute
,并在React component函数上设置async,我会按顺序获取数据,但这会与使用一些道具包装我的组件的HOC产生冲突