27

I have a table in dynamoDB called 'Contributors'. I have a primary composite key where the hash key is 'UserId' and the sort key is 'NoteId'. I want to query for all the items belonging to a particular hashkey.

Now if I use aws-cli, following command works:

aws dynamodb query \
--table-name Contributors \
--key-condition-expression 'UserId = :UserId' \
--expression-attribute-values '{
    ":UserId": {"N":"2"}
}'

But when I write the query in Node.js, neither of the below 2 param objects work:

var params = {
    TableName: "Contributors",
    KeyConditionExpression: "#UserId = :UserId",
    ExpressionAttributeNames: {
        "#UserId": "UserId"
    },
    ExpressionAttributeValues: {
        ":UserId": { "N": "2" }
    }
};

OR this:

var params = {
    TableName: "Contributors",
    KeyConditionExpression: "#UserId = :UserId",
    ExpressionAttributeNames: {
        "#UserId": "UserId"
    },
    ExpressionAttributeValues: {
        ":UserId": "2"
    }
};

I get the following error:

ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type

What should be the correct param object?

vishalaksh
  • 2,054
  • 5
  • 27
  • 45

4 Answers4

45

The following code should work. Just give the value without double quotes. The DocumentClient will automatically interpret the data type.

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
    TableName: "Contributors",
    KeyConditionExpression: "#UserId = :UserId",
    ExpressionAttributeNames: {
        "#UserId": "UserId"
    },
    ExpressionAttributeValues: {
        ":UserId": 2
    }
};

docClient.query(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});
notionquest
  • 37,595
  • 6
  • 111
  • 105
  • 3
    This solved it for me! Seems like the AWS SDK docs for Javascript refers to the AWS.DynamoDB() class. I had this problem because I was trying to implement the SDK docs examples with AWS.DynamoDB.DocumentClient(). – alanionita Sep 30 '20 at 15:31
39

I had the issue because of passing the data type along with the value. Just remove the data type DocumentClient will automatically interpret the data type and it works:

From:

ExpressionAttributeValues: {
 ":UserId": { "S": req.query.status }
}

To:

ExpressionAttributeValues: {
 ":UserId": req.query.status
}
stayingcool
  • 2,324
  • 1
  • 21
  • 24
12

Replacing:

var docClient = new AWS.DynamoDB.DocumentClient();

With:

var docClient = new AWS.DynamoDB();

Instantly solved this problem for me.

James Shapiro
  • 4,805
  • 3
  • 31
  • 46
  • 1
    This works, but makes breaks if you use docClient.get. It also includes the types in the resulting document which is a bit annoying. – DaafVader Sep 08 '20 at 09:40
3

Just in case you came here and you are using Indexes, if your index is set to string and your data it's a Number, you need to delete that Index and create a new one with the correct data type. :)

Javier Rojas
  • 165
  • 6