Javascript 如何使用CognitoIdentityCredentials获取asyc get
在用新的AWS.CognitoIdentityCredentials声明Javascript 如何使用CognitoIdentityCredentials获取asyc get,javascript,node.js,typescript,amazon-cognito,Javascript,Node.js,Typescript,Amazon Cognito,在用新的AWS.CognitoIdentityCredentials声明cred对象之后,我继续使用.get方法获取凭据并将它们存储在IdentityId变量中。运行此函数时,IdentityId变量显示为undefined。看来 console.log(“…IdentityId:”,IdentityId)在查询credenitals之前进行计算 是否有一种方法可以使用wait首先获取凭据,然后移动到console.log命令 async function GetCredentials(emai
cred
对象之后,我继续使用.get
方法获取凭据并将它们存储在IdentityId
变量中。运行此函数时,IdentityId
变量显示为undefined
。看来
console.log(“…IdentityId:”,IdentityId)代码>在查询credenitals之前进行计算
是否有一种方法可以使用wait
首先获取凭据,然后移动到console.log
命令
async function GetCredentials(email, password, callback) {
let response = await signIn(email, password);
console.log("...response:", response);
let idToken = response.response.token.idToken;
let logins = {};
logins[
"cognito-idp." + AWS_REGION + ".amazonaws.com/" + AWS_COGNITO_USER_POOL_ID
] = idToken;
console.log("...logins:", logins);
let creds = new AWS.CognitoIdentityCredentials({
IdentityPoolId: AWS_COGNITO_IDENTITY_POOL_ID,
Logins: logins
});
console.log("...creds:", creds);
let IdentityId;
creds.get(function(err) {
console.log("--------------------------")
console.log('creds.data:', creds.data);
IdentityId = creds.data.IdentityId;
console.log("--------------------------")
});
}
你可以试试
const promise = new Promise((resolve) => {
creds.get(function(err) {
console.log("--------------------------")
console.log('creds.data:', creds.data);
const IdentityId = creds.data.IdentityId;
resolve(IdentityId)
console.log("--------------------------")
});
})
const returned = await promise
但是我不确定你到底在哪里使用了IdentityId
,因为你的声明
似乎是console.log(“…IdentityId:”,IdentityId)代码>在查询credenitals之前进行计算
你的代码中没有你能把它包装成一个承诺吗?请将你的解决方案作为答案发布,以便我们投票表决。请解释你是如何意识到新承诺((resolve)=>{creds.get(function(err){…
应该在这里使用,而不是由某人提供的wait creds.get().Promise()
?@字母数字我实际上不知道该软件包的API,这就是为什么我在评论中提出了一个建议。.promise()
只有在它作为可链接函数实际存在时才起作用