aws-sdk – inserting item into dynamodb table from lambda function result in error due item attribute type mismatch

I have a simple lambda function that is trying to insert an intem into a dynamodb table.

The said table has its PK of type string, I’m trying to insert a string in it, but I get an error that said I’m giving a Map.

My code is roughly this:

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();
}

Below is execution log:

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật