amazon-web-services - DynamoDB 获取项目 TypeScript hell

标签 amazon-web-services aws-lambda amazon-dynamodb aws-sdk aws-sdk-js

谁能解释一下如何使用GetItemInput打电话时输入DocumentClient.get ?

如果我传入任何类型的对象 get有效,但如果我尝试强烈键入 params 对象,我会收到此错误:

ValidationException: The provided key element does not match the schema

这是我的 lambda 函数代码,其中我将参数作为 any 类型传递:

export const get: Handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {

  console.log(event.pathParameters)
  if (!event.pathParameters) {
    throw Error("no path params")
  }

  const params: any = {
    Key: {
      id: event.pathParameters.id
    },
    TableName: table
  }

  console.log(params)
  try {
    const result: any = await dynamoDb.get(params).promise()
    return {
      body: JSON.stringify(result.Item),
      statusCode: result.$response.httpResponse.statusCode
    }

  } catch (error) {
    console.log(error)
    return {
      body: JSON.stringify({
        message: `Failed to get project with id: ${event.pathParameters!.id}`
      }),
      statusCode: 500
    }
  }
}

这是我试图让它与类型 GetItemInput 一起工作的尝试。

export const get: Handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {

  console.log(event.pathParameters)
  if (!event.pathParameters) {
    throw Error("no path params")
  }

  const params: GetItemInput = {
    Key: {
      "id": { S: event.pathParameters.id }
    },
    TableName: table
  }

  console.log(params)
  try {
    const result: any = await dynamoDb.get(params).promise()
    return {
      body: JSON.stringify(result.Item),
      statusCode: result.$response.httpResponse.statusCode
    }

  } catch (error) {
    console.log(error)
    return {
      body: JSON.stringify({
        message: `Failed to get project with id: ${event.pathParameters!.id}`
      }),
      statusCode: 500
    }
  }
}

如果我离开 Key和以前一样阿拉:

const params: GetItemInput = {
  Key: {
    id: event.pathParameters.id
  },
  TableName: table
}

不出所料,我得到一个类型错误。但无法理解如何形成我的Key这样我就没有得到 ValidationException .

注意 id字段的类型为 StringDynamoDB .

最佳答案

我认为您混合了两个不同的客户端定义文件 DynamoDB DynamoDB.DocumentClient .当您使用 DynamoDB.DocumentClient客户端,同时你正在使用接口(interface)DynamoDB.Types.GetItemInput来自 DynamoDB .

您应该使用 DynamoDB.DocumentClient.GetItemInput :

import {DynamoDB} from 'aws-sdk';
const dynamo = new DynamoDB.DocumentClient({apiVersion: '2012-08-10'});

...
const params: DynamoDB.DocumentClient.GetItemInput = {
    TableName: table,
    Key: {
        id: event.pathParameters.id
    }
};
const result = await this.dynamo.get(params).promise();

关于amazon-web-services - DynamoDB 获取项目 TypeScript hell ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167259/

相关文章:

amazon-web-services - 如何获取 Amazon S3 对象的最新版本 ID?

aws-lambda - 如何在 AWS Lambda 中验证客户端证书

java - 在 aws lambda 上使用 Log4J2 进行记录 - 找不到类

ruby-on-rails - 在Rails中使用Amazon Dynamo DB时如何实现分页

elasticsearch - 从S3加载数据以进行排序并允许时间线分析

amazon-s3 - 使用 EMR 跨不同账户复制 dynamoDB 表

python - 如何在python中异步发送SQS消息?

amazon-web-services - AWS Data Pipeline - 尝试重新运行失败的事件时出错

linux - 如何找出我在 Amazon EC2 上的存储空间已满的原因?

typescript - 在 AWS Lambda 和 AWS Cloudwatch 中使用 Typescript 源映射