首页 > 解决方案 > DynamoDB query shows UnepectedParameter

问题描述

I'm trying to create a query in Lambda for a DynamoDB table.

I have followed the Instructions and examples from the aws developer handbook but still get an error

//+++++++++++++++++++++++++++++++++++++++++++
//The database Structure

 "Table": {
    "AttributeDefinitions": [
      {
        "AttributeName": "Score",
        "AttributeType": "N"
      },
      {
        "AttributeName": "gtin_RecBook",
        "AttributeType": "S"
      },
      {
        "AttributeName": "gtin_RefBook",
        "AttributeType": "S"
      }
    ],
    "TableName": "Recommendation_test_2",
    "KeySchema": [
      {
        "AttributeName": "gtin_RefBook",
        "KeyType": "HASH"
      },
      {
        "AttributeName": "gtin_RecBook",
        "KeyType": "RANGE"
      }
    ]
}

//+++++++++++++++++++++++++++++++++++++++++++
// The Lambda Code

const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region:'eu-central-1', apiVersion:'2012-08-10'});

exports.handler = (event, context, callback) => {
    const params = {
        TableName: "Recommendation_test_2",
        KeyConditionExpression: "gtin_RefBook = :a",
        ExpressionAttributeValues: {
            ":a": "9783453471092"
        }
    };
    dynamodb.query(params, function(err, data){
      if (err) {
          console.log(err);
          callback(err);
      }  else{
          console.log(data);
          callback(null, data);
      }
    });
};

The database is populated and should provide some results.

However Lambda throws the following error: "errorMessage": "There were 14 validation errors:\n* InvalidParameterType: Expected params.ExpressionAttributeValues[':a'] to be a structure"

Could you please tell me what to change to make the query work?

标签: aws-lambdaamazon-dynamodb

解决方案


您需要使用DynamoDB Document Client或将 ExpresionAttributeValue 更改为

ExpressionAttributeValues: {
  ":a": {
    "S": "9783453471092"
  }
}

推荐阅读