访问对象时出现Java脚本错误

时间:2018-09-11 23:19:28

标签: javascript amazon-web-services aws-lambda aws-sdk

我有用于处理AWS响应的javaScript

async function getUserOfAuthenticatedUser(event) {
    let userSub = event.requestContext.identity.cognitoAuthenticationProvider.split(':CognitoSignIn:')[1]
    let request = {
        UserPoolId: 'xxxxxxxxxxxx', // Set your cognito user pool id
        Filter: `sub = "${userSub}"`,
        Limit: 1
    }
    let users = await cognitoClient.listUsers(request).promise();
    console.log("got user:", users['Users']['Attributes']);
}

执行代码时,出现以下错误

UserCreateDate: 2018-09-10T22:58:01.414Z,
                                ^^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)

如果我打印,则用户输出为

{ Users: 
   [ { Username: '7595c6c3-b29b-4fd5-aeb4-45c0ad2669b8',
       Attributes: [Array],
       UserCreateDate: 2018-09-10T22:58:01.414Z,
       UserLastModifiedDate: 2018-09-10T22:58:23.901Z,
       Enabled: true,
       UserStatus: 'CONFIRMED' } ] }

如何访问属性?

1 个答案:

答案 0 :(得分:1)

结果是一个对象,其属性为Users,是一个Objects的数组,每个对象都有一个属性Attributes,它是一个数组。

您可以在API reference

中看到更多示例

要访问返回列表中第一个用户的属性列表,请执行以下操作:

console.log(users['Users'][0]['Attributes'])