skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    Found the problem, I need to use the PutItemCommand' from the same library of the client instead of the PutCommandfrom@aws-sdk/lib-dynamodb`.

    Below the updated code

    const { DynamoDBClient, PutItemCommand } = require("@aws-sdk/client-dynamodb");
    
    const client = new DynamoDBClient({});
    
    const commandInput = {
        TableName: process.env.TABLE_NAME,
    };
    
    exports.handler = async (event) => {
        commandInput.Item = {
            title_hash: { "S": strip(event.title)},
            album_title: { "S": event.album_title },
            number: { "N": event.number.toString() },
            title: { "S":  event.title },
            length: { "N": event.length.toString() },
            artist_name: { "S":  event.artist_name },
            cover_url: { "S":  event.cover_url},
            plays_total: { "N": "0" },
        };
    
        const commandResult = new PutItemCommand(commandInput);
    
        const body = await client.send(commandResult);
    
    };
    
    function strip(inputString) {
        const stripped = inputString.replace(/[^a-z0-9]/gi, '').toLowerCase();
        return stripped;
    }
    

  2. You’re using the high level document client but passing your data in the low level format. Have a read of this blog to understand in more detail.

    Change this:

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

    To this:

    const client = new DynamoDBClient({});
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search