【问题标题】:export values from dynamoDB getItem从 dynamoDB getItem 导出值
【发布时间】:2021-03-13 04:58:20
【问题描述】:

我正在尝试从 dynamoDB 中获取 getItem 函数之外的数据,但我遇到了这个错误,我不知道为什么。

Error ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes

这是我的代码

const aws = require("aws-sdk"),
  docClient = new aws.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" }),
  ddb = new aws.DynamoDB({ apiVersion: "2012-08-10" }),
  tableName = "usersDB",

 exports.apiKey = async (req, res, context) => {
  var params = {
    TableName: tableName,
    Key: {
      "username": { "S": req.body.username },
    },
    ProjectionExpression: "apiKey"
  };

  chaveAPI = await ddb.getItem(params, function (err, data) {
    if (err) {
      console.log("Error", err);
    } else {
      console.log("Success", data.Item);
    }
  });
};

EDIT1:修复了验证错误,但仍然无法从 dyname 获取数据。这里是固定的

exports.apiKey = async (req, res) => {
    console.log("comecei")

    var params = {
        TableName: tableName,
        Key: {
          'username': {S: req.user.username}
        },
        ProjectionExpression: 'apiKey'
      };
      
      // Call DynamoDB to read the item from the table
      dataFromDynamo = await ddb.getItem(params, function(err, data) {
        if (err) {
          console.log("Error", err);
        } else {
          console.log("Success", data.Item);
          return data.Item
        }
      });

console.log(dataFromDynamo)

};

【问题讨论】:

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


    【解决方案1】:

    我注意到您的代码示例的几件事:

    1. 您正在将回调与 async/await 混合使用。
    2. 您没有声明 dataFromDynamo 变量。

    不是这个

         // Call DynamoDB to read the item from the table
         dataFromDynamo = await ddb.getItem(params, function(err, data) {
            if (err) {
              console.log("Error", err);
            } else {
              console.log("Success", data.Item);
              return data.Item
            }
          });
    

    这样做

    try {
        // Call DynamoDB to read the item from the table
        const dataFromDynamo = await ddb.getItem(params).promise()
        console.log("Success", data.Item);
        return data.Item
    } catch (err) {
        console.log("Error", err);
    }
    

    【讨论】:

    • 感谢您的回答。我是异步的新手。在我发现一些教程之后,我得到了它像你发布的那样工作。
    • 太棒了!如果我的回答有帮助,请考虑接受它作为答案,以便它可以帮助遇到相同问题的其他人。
    • 对不起。我ḿ Stack 上的新功能。仍在学习它的绳索
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2022-01-21
    • 2013-09-24
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    相关资源
    最近更新 更多