John Leszcz John Leszcz - 5 months ago 230
JSON Question

Node.js communcation with DynamoDB- JSON

I try to get data from DynamoDB which userLimit is between lower and higher value. I send lower value to my code with JSON. My Node.js code:

var AWS = require('aws-sdk');
var db = new AWS.DynamoDB();


exports.handler = function(event, context) {

var low=event.ke1;

var params = {
TableName: "Events", //"StreamsLambdaTable",
ProjectionExpression: "userLimit, description", //specifies the attributes you want in the scan result.
FilterExpression: "userLimit between :lower and :higher",
// ExpressionAttributeNames: {
// "#yr": "year",
// },
ExpressionAttributeValues: {
":lower": {"N": "low"},
":higher": {"N": "50"}
}
};

db.scan(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
data.Items.forEach(function(record) {
console.log(
record.description.S + "");
});
context.succeed(data.Items);
// context.done(null,{"Result": "Operation succeeded."});
//res.send(data.name);
}
// return next();
});
};


I get an error:

[ValidationException: ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: low for key :lower]
message: 'ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: low for key :lower',


If I change
":lower": {"N": "low"},
to
":lower": {"N": low},
error is:

{ [ValidationException: ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :lower]
message: 'ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :lower',


My JSON data is:

{
"key1": "1"
}


How to set that lower value with the value from the key1?

EDIT:

There was mistake in:

var low=event.ke1;


Shuld be

var low=event.key1;


EDIT 2:

Another error occured when I was running that code:

var params = {
TableName: "Events", //"StreamsLambdaTable",
ProjectionExpression: "userLimit, description, type", //specifies the attributes you want in the scan result.
KeyConditionExpression: "type = :tempType",
// ExpressionAttributeNames: {
// "#yr": "year",
// },
ExpressionAttributeValues: {
":tempType": {"S": "Party"}
}
};


Error:

{ [UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params]
message: 'Unexpected key \'KeyConditionExpression\' found in params',

Answer

"KeyConditionExpression" is not applicable for a scan operation.

You need to change your operation to query, if the attribute is an idexed field or use FilterExpression if it has to be a scan operation.