简体   繁体   English

使用 NodeJs 的 AWS DynamoDB 查询

[英]AWS DynamoDB Query with NodeJs

I have a AWS DynamoDB table with columns email(partition key) and password.我有一个 AWS DynamoDB 表,其中包含电子邮件(分区键)和密码列。 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.我正在使用 JavaScript(NodeJs) AWS SDK 进行集成。
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 参考文件

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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM