3

I have a AWS DynamoDB table with columns email(partition key) and password. I want to run a query with fetch matching records with combination of giving email and password.
I am using JavaScript(NodeJs) AWS SDK for integration.
But I am facing some challenges while executing my query, below are my code block which I am using-

var params = {
    TableName : "tblUsers",
    KeyConditionExpression : 'email = :emailValue', 
    FilterExpression : '#password= :passwordValue',
     ExpressionAttributeNames : {
        '#password' : 'password'
    },
    ExpressionAttributeValues : {
        ':emailValue' : email,
        ':passwordValue' : password
    }
};

dynamodb.query(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    //console.log("Success", data.Items);
    data.Items.forEach(function(element, index, array) {
      console.log(element);
    });
  }
});

Below are errors I am getting -
Error MultipleValidationErrors: There were 8 validation errors:
* InvalidParameterType: Expected params.ExpressionAttributeValues[':value'] to be a structure
* UnexpectedParameter: Unexpected key '0' found in params.ExpressionAttributeValues[':value']
* UnexpectedParameter: Unexpected key '1' found in params.ExpressionAttributeValues[':value']
* UnexpectedParameter: Unexpected key '2' found in params.ExpressionAttributeValues[':value']
* UnexpectedParameter: Unexpected key '3' found in params.ExpressionAttributeValues[':value']
* UnexpectedParameter: Unexpected key '4' found in params.ExpressionAttributeValues[':value']
* UnexpectedParameter: Unexpected key '5' found in params.ExpressionAttributeValues[':value']
* UnexpectedParameter: Unexpected key '6' found in params.ExpressionAttributeValues[':value']

Reference Document

Rahul Gaikwad
  • 563
  • 4
  • 9
  • 26
  • 1
    You haven't shown all the relevant code but I'm guessing that you have `const dynamodb = new AWS.DynamoDB()` when you should have used `const dynamodb = new AWS.DynamoDB.DocumentClient()`. There are two levels of DynamoDB API. Can you try that first? – jarmod Sep 24 '20 at 20:31
  • 1
    I am using the same - var dynamodb = new AWS.DynamoDB(); – Rahul Gaikwad Sep 25 '20 at 18:18
  • 1
    Minor comment: with vanishingly few exceptions, `var` should no longer be used. The bigger point is that it's important to understand the difference between the regular DynamoDB client and the [DocumentClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) - the former requires you to explicitly indicate attribute types e.g. `email: { 'S': 'joe@example.com' }` while the latter understands native JavaScript types e.g. `email: 'joe@example.com'`. – jarmod Sep 25 '20 at 20:11

1 Answers1

6

Try something like this instead:

const AWS = require("aws-sdk");

const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-west-2" });

const query = async () => {
  const response = await documentClient
    .query({
      TableName: "tblUsers",
      ExpressionAttributeNames: {
        "#password": "password",
        "#email": "email"
      },
      ExpressionAttributeValues: {
        ":emailValue": "email",
        ":passwordValue": "password",
      },
      FilterExpression: "#password = :passwordValue",
      KeyConditionExpression: "#email = :emailValue",
    })
    .promise();

  console.log(`Query response: ${JSON.stringify(response, null, 2)}`);
};

query().catch((error) => console.error(JSON.stringify(error, null, 2)));
NoSQLKnowHow
  • 4,449
  • 23
  • 35