扫描DynamoDB表未显示ProjectionExpression中指定的列的结果

时间:2018-05-22 08:15:03

标签: node.js aws-lambda amazon-dynamodb alexa-skills-kit alexa-skill

我正在尝试扫描DynamoDB故事,以便我只获取已过滤的项目。 myTable有3列:L时间戳(String,PK),colors(String),userId(String)

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

var params = {
    "TableName": "myTable",
    "ProjectionExpression": "colors, userId",
    "ExpressionAttributeValues": {":val": userId},
    "FilterExpression": "userId = :val"

};

console.log("Scanning table.");
docClient.scan((params), function(err,data){

    if (err) console.log(err, err.stack); 
    else console.log(data); //success response
});
  

2018-05-22T08:04:21.395Z baac6d92-5d96-11e8-ae78-bd04c275acf5扫描台。   2018-05-22T08:04:21.672Z baac6d92-5d96-11e8-ae78-bd04c275acf5 {Items:[{userId:' amzn1.ask.account.XYZ' }],Count:1,ScannedCount:2}

因此,我只能从userId列获取值。专栏颜色'被完全忽略了。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

ProjectionExpression中设置ExpressionAttributeNames个属性。请查看以下代码段

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

 var params = {
  ExpressionAttributeNames: {
   "#colors": "colors", 
   "#userId": "userId"
  }, 
  ExpressionAttributeValues: {
    ":val": userId
  }, 
  FilterExpression: "userId = :val", 
  ProjectionExpression: "#colors, #userId", 
  TableName: "myTable"
 };

console.log("Scanning table."); 

docClient.scan(params, function(err, data) {
    if (err) console.log(err, err.stack); 
    else console.log(data); //success response
});

请注意,#:必须分别出现在ExpressionAttributeNamesExpressionAttributeValues上。