【问题标题】:Unable to update the item in DynamoDB using the GSI无法使用 GSI 更新 DynamoDB 中的项目
【发布时间】:2022-01-30 02:35:15
【问题描述】:

我的 dynamodb 表中有以下结构:- id:partition_key user_id: GSI

以下是我的 Lambda,我试图在用户对象(JSON 格式)中从客户端获取 user_id 的值,然后尝试更新 Dynamodb使用查询操作的项目但它不起作用:-

exports.lambdaHandler = async (event, context) => {
  logger.info('event:', event);
  let body, data;
  let statusCode = 200;
  const headers = {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Headers': 'Content-Type',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': '*'
  };
  const user = JSON.parse(event.body).user;
  const updatedOn = new Date().getTime();
  try {
    const params = {
      TableName: ddbTable,
      IndexName: 'user_id-index',
      KeyConditionExpression: 'user_id = :id',
      ExpressionAttributeValues: {
        ':id': user.id
      }
    };

    dynamodb.query(params, function (err, result) {
      console.log("result", result);
      data = result;
      if (err) {
        console.log("Unable to query item. Error JSON:", JSON.stringify(err));
      } else {
        const parameters = {
          TableName: ddbTable,
          Key: {
            "id": result.Items[0].id
          },
          UpdateExpression: "set first_name = :f, last_name = :l, postal_code = :p, country_code = :c, updated_on = :u",
          ExpressionAttributeValues: {
            ":f": user.firstName,
            ":l": user.lastName,
            ":p": user.postalCode,
            ":c": user.countryCode,
            ":u": updatedOn
          },
          ReturnValues: "UPDATED_NEW"
        };
        dynamodb.update(parameters, function (err, data) {
          if (err) console.log(err);
          else console.log(data);
        });
      }
    });
    console.log("*********");
  } catch (err) {
    console.log(err);
    statusCode = 400;
    body = err.message;
    return err;
  } finally {
    body = JSON.stringify(data);
  }
  return {
    statusCode,
    body,
    headers
  };
};

查询操作根本不起作用。 该语句没有被执行

dynamodb.query(params, function (err, result) {

请提出建议。

【问题讨论】:

  • 什么不起作用?您收到什么错误消息? CloudWatch 日志中有什么内容?
  • @fedonev,它只是打印这些日志 console.log("params", params);和 console.log("*********");在 CloudWatch 上。 Lambda 无法查询 DynamoDB 即此语句未执行 dynamodb.query(params, function (err, result) {

标签: node.js amazon-dynamodb dynamodb-queries amazon-dynamodb-index


【解决方案1】:

在 Node.js Lambda 处理程序中有 two patterns to invoke asynchronous calls,例如 dynamodb.query:async-await 或回调。由于您的处理程序均未正确实现,因此 Lambda 在 SDK 调用未完成的情况下返回。

推荐的方法是异步等待。添加await 关键字并使用.promise() 承诺该方法。这种模式是首选,因为它消除了嵌套 SDK 调用的“回调地狱”。

const queryResult = await dynamodb.query(params).promise()

const updateParams = {
  Key: { "id": queryResult.Items[0].id },
  ...
}

const updateResult = await dynamodb.update(updateParams).promise()

有关回调模式示例,请参阅上面的链接或来自 awsdocs 存储库的 this

注意:AWS SDK for JavaScript v3 现在是稳定的推荐版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2017-03-01
    • 2019-02-16
    相关资源
    最近更新 更多