drm18272
drm18272

Reputation: 11

AWS Lambda scanning a dynamoDB table

Im trying to scan a dynamodb table for all entries with prices between 1 and 13,

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


exports.handler = function(event, context) {

  var params = {
    TableName: "hexagon2",
    ProjectionExpression: "price", 
    FilterExpression: "price between :lower and :higher",
    ExpressionAttributeValues: {
     ":lower": {"N": "1"},
     ":higher": {"N": "13"}
    }
};

  db.scan(params, function(err, data) {
    if (err) {
      console.log(err); // an error occurred
      } 
    else {
      console.log(data.Item); // successful response
      context.done(null,{"Result": "Operation succeeded."});
      }
  });
};

But I always get the following error, when I test it. I definatly have 'price' as a number attribute in my table and IAM is set up too.

START RequestId: f770c78b-93a1-11e6-b5f6-e5c31cef8b2d Version: $LATEST
2016-10-16T13:10:54.299Z    f770c78b-93a1-11e6-b5f6-e5c31cef8b2d    undefined
END RequestId: f770c78b-93a1-11e6-b5f6-e5c31cef8b2d
REPORT RequestId: f770c78b-93a1-11e6-b5f6-e5c31cef8b2d  Duration: 912.58 ms Billed Duration: 1000 ms    Memory Size: 128 MB Max Memory Used: 24 MB  

Upvotes: 1

Views: 1013

Answers (1)

Mark B
Mark B

Reputation: 200476

You are trying to reference data.Item which is undefined. Scan operations return a list, not a single item. That list would be referenced via data.Items

When in doubt, read the documentation. Or you could just print out the entire data response to see the exact format of the response coming back.

Upvotes: 2

Related Questions