Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AWS放大,等待Auth.currentUserInfo_Javascript_Aws Amplify - Fatal编程技术网

Javascript AWS放大,等待Auth.currentUserInfo

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

我需要从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
上的真实数据呢?

在您调用
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产生冲突