aws-sdk - 从 lambda 函数将项目插入 dynamodb 表会因项目属性类型不匹配而导致错误

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

我有一个简单的 lambda 函数,它试图将 intem 插入 dynamodb 表中。

该表的 PK 类型为

string
,我试图在其中插入一个字符串,但收到一条错误消息,表示我正在提供一个地图。

我的代码大致是这样的:

const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");

const {
    DynamoDBDocumentClient,
    PutCommand,
} = require("@aws-sdk/lib-dynamodb")

const client = DynamoDBDocumentClient.from(
    new DynamoDBClient({})
);

const putItemCommandInput = {
    TableName: process.env.TABLE_NAME,
};

// following code is in the function handler

putItemCommandInput.Item = {
    title_hash: { "S": strip(event.title)}, // pk
    album_title: { "S": event.album_title }, // sk
    number: { "N": event.number },
    title: { "S":  event.title },
    length: { "N": event.length },
    artist_name: { "S":  event.artist_name },
    cover_url: { "S":  event.cover_url},
    plays_total: { "N": "0" },
};

console.log('Storing new song');
console.log(putItemCommandInput.Item);

const PutItemCommand = new PutCommand(putItemCommandInput);

const body = await client.send(PutItemCommand);


// separate function to remove unwanted characters, this DO return a string
function strip(inputString) {
    return inputString.replace(/[^a-z0-9]/gi, '').toLowerCase();
}

以下是执行日志:

2024-04-26T10:40:02.070Z    81327c7f-65bd-4af6-b62f-178c39264084    INFO    Storing new song
2024-04-26T10:40:02.070Z    81327c7f-65bd-4af6-b62f-178c39264084    INFO    {
  title_hash: { S: '742617000027' },
  album_title: { S: 'Slipknot' },
  number: { N: 1 },
  title: { S: '742617000027' },
  length: { N: 36 },
  artist_name: { S: 'Slipknot' },
  cover_url: {
    S: 'https://i.scdn.co/image/ab67616d0000b273baf04a1d30db6ac9de26da7d'
  },
  plays_total: { N: '0' }
}
2024-04-26T10:40:02.410Z    81327c7f-65bd-4af6-b62f-178c39264084    ERROR   Invoke Error    {"errorType":"ValidationException","errorMessage":"One or more parameter values were invalid: Type mismatch for key title_hash expected: S actual: M","name":"ValidationException","$fault":"client","$metadata":{"httpStatusCode":400,"requestId":"2BEM4S6PUNFUEIJ826L49C8TQ7VV4KQNSO5AEMVJF66Q9ASUAAJG","attempts":2,"totalRetryDelay":91},"__type":"com.amazon.coral.validate#ValidationException","message":"One or more parameter values were invalid: Type mismatch for key title_hash expected: S actual: M","stack":["ValidationException: One or more parameter values were invalid: Type mismatch for key title_hash expected: S actual: M","    at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)","    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5","    at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-dynamodb/dist-cjs/index.js:2150:14)","    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20","    at async /var/runtime/node_modules/@aws-sdk/lib-dynamodb/dist-cjs/index.js:158:30","    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/core/dist-cjs/index.js:165:18","    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38","    at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22","    at async exports.handler (/var/task/index.js:48:18)"]}
END RequestId: 81327c7f-65bd-4af6-b62f-178c39264084
amazon-web-services aws-lambda amazon-dynamodb aws-sdk aws-sdk-js
1个回答
0
投票

您正在使用高级文档客户端,但以低级格式传递数据。阅读此博客以更详细地了解。

更改此:

const client = DynamoDBDocumentClient.from(
    new DynamoDBClient({})
);

对此:

const client = new DynamoDBClient({});
© www.soinside.com 2019 - 2024. All rights reserved.