【问题标题】:AWS DynamoDB Query with NodeJs使用 NodeJ 进行 AWS DynamoDB 查询
【发布时间】:2021-01-11 04:09:51
【问题描述】:

我有一个 AWS DynamoDB 表,其中包含电子邮件(分区键)和密码列。 我想运行一个查询,并结合提供电子邮件和密码来获取匹配记录。
我正在使用 JavaScript(NodeJs) AWS SDK 进行集成。
但是我在执行查询时面临一些挑战,下面是我正在使用的代码块 -

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

以下是我遇到的错误 -
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

【问题讨论】:

  • 您还没有显示所有相关代码,但我猜您应该使用const dynamodb = new AWS.DynamoDB.DocumentClient() 时拥有const dynamodb = new AWS.DynamoDB()。 DynamoDB API 有两个级别。你能先试试吗?
  • 我正在使用相同的 - var dynamodb = new AWS.DynamoDB();
  • 次要评论:除了极少数例外,不应再使用var。更重要的一点是,了解常规 DynamoDB 客户端和 DocumentClient 之间的区别很重要——前者要求您明确指出属性类型,例如email: { 'S': 'joe@example.com' } 而后者理解原生 JavaScript 类型,例如email: 'joe@example.com'.

标签: node.js amazon-web-services amazon-dynamodb dynamodb-queries


【解决方案1】:

试试这样的方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多