我的 AWS lambda 函数无法与使用 aws sdk 版本 3 的 Dynamo DB getItem 配合使用

问题描述 投票:0回答:1

我无法使用 aws sdk 版本 3 获得基本 dynamodb 与 aws lambda 函数集成的解决方案。下面是我的 index.mjs 代码。

    import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";
//import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
const dbclient = new DynamoDBClient({region:'us-east-1'});
export const handler = async (event) => {
  const uid=event.id;
  console.log("id from test event",uid);
  // TODO implement
  const params={
       TableName: 'test_cc_auth',
             Key: {
                 id: { S: uid }
                //id:  uid 
            }
          };
   // verify the db has data or not
     console.log("params object is ",params);
   var datafound;
   try{
      datafound= await dbclient.send(new GetItemCommand(params));
     console.log("data is : ",datafound);
   }catch(err){
      console.log(err);
   }
  const response = {
    statusCode: 200,
    body: JSON.stringify(datafound),
  };
  return response;
};

我收到以下错误。“ValidationException:提供的关键元素与架构不匹配”,但是当我修改代码参数对象(如 id:uid)时,我收到“TypeError:无法读取未定义的属性(读取'0”) ')==== 不提及 S"。有人可以帮忙解决这个问题以及添加类型 S 和添加类型之间有什么区别吗?谢谢您的支持。

aws-lambda amazon-dynamodb
1个回答
0
投票

在不知道表的架构的情况下无法确定,但

test_cc_auth
表很可能同时具有分区键和排序键。在这种情况下,您需要同时提供它们,即如下所示:

const params={
   TableName: 'test_cc_auth',
   Key: {
      id: { S: uid },
      other_id: { S: "value"}
   }
};

其中

other_id
应替换为排序键的名称。

来源:DynamoDB 文档

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.