简体   繁体   中英

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.

It sounds like my parameters for my query into DynamoDB is not valid.

My Error: dynamodb.html:150 ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type

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. Also I tried age instead because it is another attribute I have, but I get the same initial error as above. So I've deleted ProjectionExpression: altogether. 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. 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.

Sign in and query with IAM role

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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