Bird
0
0

You wrote this code to put an item into DynamoDB using the SDK, but it throws an error:

medium📝 Debug Q14 of 15
DynamoDB - with AWS SDK
You wrote this code to put an item into DynamoDB using the SDK, but it throws an error:
const { DynamoDBClient, PutItemCommand } = require("@aws-sdk/client-dynamodb");
const client = new DynamoDBClient({ region: "us-east-1" });

async function addItem() {
  const command = new PutItemCommand({
    TableName: "Products",
    Item: {
      ProductId: "001",
      Name: "Book"
    }
  });
  await client.send(command);
}

addItem();

What is the likely cause of the error?
ATableName should be lowercase.
BDynamoDBClient cannot be used with PutItemCommand.
CPutItemCommand requires a callback function.
DItem attributes must specify data types like { S: "001" } for strings.
Step-by-Step Solution
Solution:
  1. Step 1: Check Item attribute format

    DynamoDB SDK expects each attribute value to be an object specifying its type, e.g., { S: "string" }.
  2. Step 2: Identify error cause

    Passing plain strings without type wrappers causes the SDK to throw an error.
  3. Final Answer:

    Item attributes must specify data types like { S: "001" } for strings. -> Option D
  4. Quick Check:

    Attributes need type wrappers = B [OK]
Quick Trick: Wrap attribute values with type objects like { S: "value" } [OK]
Common Mistakes:
MISTAKES
  • Using plain strings instead of typed objects
  • Changing table name case incorrectly
  • Expecting callbacks instead of promises

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes