1

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  

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That returns [ { price: { N: '12' } }, { price: { N: '10' } } ] (Which is the prices of the two items that meet the criteria, Not the whole items with their names etc. Why does it do this even though It get the whole data.Items ?
With DynamoDB you have to tell it what attributes you want back. If you want all the attributes back you need to add Select: "ALL_ATTRIBUTES" to your params object. This is all spelled out clearly in the documentation. I really suggest you read the documentation. At the very least you should go here docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/… and look at all the parameters the query function takes and what each parameter does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.