简体   繁体   English

DynamoDB数据库查询中的无效参数-Javascript-AWS

[英]Invalid params in DynamoDB database query - Javascript - AWS

I'm using AWS to make database queries into dynamoDB with a Cognito user/IAM roles and policies. 我正在使用AWS通过Cognito用户/ IAM角色和策略对dynamoDB进行数据库查询。

It sounds like my parameters for my query into DynamoDB is not valid. 听起来我查询DynamoDB的参数无效。

My Error: dynamodb.html:150 ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type 我的错误:dynamodb.html:150 ValidationException:一个或多个参数值无效:条件参数类型与架构类型不匹配

I've tried adding: ProjectionExpression: "name" because it's one of my attributes, but name is a reserved word apparently so I can't use that. 我尝试添加: ProjectionExpression: "name"因为它是我的属性之一,但是name显然是保留字,因此我不能使用它。 Also I tried age instead because it is another attribute I have, but I get the same initial error as above. 我也尝试了age因为它是我拥有的另一个属性,但是我得到了与上述相同的初始错误。 So I've deleted ProjectionExpression: altogether. 所以我已经删除了ProjectionExpression: :。 Here is what I now have for my parameters: 这是我现在需要的参数:

var params = {
        ExpressionAttributeValues: {
            ":v1": {
                N: 123456788
            }
        },
        KeyConditionExpression: "userId = :v1",
        TableName: "user"
        };

I'm not very sure how else to frame this. 我不太确定该如何构架。 Please let me know what im missing or if there's any good documentation, as I'm kind of new to AWS. 请让我知道我缺少什么或者是否有任何好的文档,因为我是AWS的新手。 Thank you in advance! 先感谢您!

EDIT: 编辑:

The primary key is userId and the rest of the attributes for the table user are: age , bio , email , name in order. 主键是userId ,表user的其余属性是: agebioemailname依次。

Sign in and query with IAM role 登录并以IAM角色查询

var data2 = {
UserPoolId: 'xxxxxxxx',
ClientId: 'xxxxxxxx',
};

var userPool = new AmazonCognitoIdentity.CognitoUserPool(data2);
var cognitoUser = userPool.getCurrentUser();

var userData = {
    Username : 'xxxxxxxx',
    Pool : userPool
    };        

var authenticationData = {
    Username : 'xxxxxxxx',
    Password : 'xxxxxxxx',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);       
var cognitoUser2 = new AmazonCognitoIdentity.CognitoUser(userData);
   cognitoUser2.authenticateUser(authenticationDetails, {
       onSuccess: function (result) {
           var accessToken = result.getAccessToken().getJwtToken(); //user sign-in success and you can get accessToken and IdToken

       //POTENTIAL: Region needs to be set if not already set previously elsewhere.
           AWS.config.region = 'xxxxxxxx';

       AWS.config.credentials = new AWS.CognitoIdentityCredentials({
           IdentityPoolId : 'xxxxxxxx', // your identity pool id here
           Logins : {
               // Change the key below according to the specific region your user pool is in.
               // Here you need to map your idToken with your user pool domain for Cognito Identity Pool to generate a Identity with credential.
               'cognito-idp.xxxxxxxx.amazonaws.com/xxxxxxxx' : result.getIdToken().getJwtToken()     
           }
       });

       //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
       AWS.config.credentials.refresh((error) => {
           if (error) {
                console.error(error);
           } else {
               console.log('Successfully logged!');
        // Instantiate aws sdk service objects now that the credentials have been updated. So put your DynamoDB client here
        var docClient = new AWS.DynamoDB.DocumentClient({ region: AWS.config.region });
        var params = {

           ExpressionAttributeValues: {
                ":v1": {
                    N: 123456788
                }
            },
            KeyConditionExpression: "userId = :v1",
            TableName: "user"
            };
          docClient.query(params, function(err, data2) {
            if (err) {
                console.error(err);
            }else{
              console.log(data2);
            }
          }); 
               }
           });
       },
    onFailure: function(err) {
           alert(err.message || JSON.stringify(err));
       },
   });

Try using it without N in the ExpressionAttributeValues and use " for the user id value. 尝试在ExpressionAttributeValues使用不带N ,并使用"作为用户ID值。

var params = {
        ExpressionAttributeValues: {
            ":v1": "123456788"
        },
        KeyConditionExpression: "userId = :v1",
        TableName: "user"
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM