简体   繁体   中英

AWS Lambda scanning dynamoDB

I want to scan DynamoDB to get records with price between 9 and 13. My code:

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


exports.handler = function(event, context) {

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

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

But I've got 2 errors:

  • InvalidParameterType: Expected params.ExpressionAttributeValues[':lower'] to be a structure
  • InvalidParameterType: Expected params.ExpressionAttributeValues[':higher'].N to be a string]

I tried two difffrent approaches (as you can see in the code:)

ExpressionAttributeValues: {
         ":lower": 9,
         ":higher": {"N": 13}
    }

but neither of them work.

EDIT:

I've got another problem. I want to get all those prices. I tried with that code:

data.Items.forEach(function(record) {
           console.log(
                record.price + " dadada");
        });

But I get:

2016-06-14T20:33:23.915Z    3ce248c7-326f-11e6-855c-57839aedb817    [object Object] dadada
2016-06-14T20:33:23.915Z    3ce248c7-326f-11e6-855c-57839aedb817    [object Object] dadada

EDIT 2:

Soolved. Just changed record.price to record.price.N

The error messages are telling you exactly what is wrong. It expects ":lower " to be a structure, just like you have provided to ":higher" , and it expects the value of ":higher" to be a string. Like so:

ExpressionAttributeValues: {
     ":lower": {"N": "9"},
     ":higher": {"N": "13"}
}

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