Bird
0
0

Given this JavaScript code snippet using DynamoDB SDK, what will be the output?

medium📝 query result Q13 of 15
DynamoDB - with AWS SDK
Given this JavaScript code snippet using DynamoDB SDK, what will be the output?
const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb");
const client = new DynamoDBClient({ region: "us-east-1" });

async function getItem() {
  const command = new GetItemCommand({
    TableName: "Users",
    Key: { "UserId": { S: "123" } }
  });
  const response = await client.send(command);
  console.log(response.Item);
}

getItem();
APrints all items in the 'Users' table.
BThrows a syntax error because of missing await.
CPrints the item with UserId '123' if it exists, otherwise undefined.
DReturns a list of UserIds from the table.
Step-by-Step Solution
Solution:
  1. Step 1: Understand GetItemCommand behavior

    GetItemCommand fetches a single item by primary key from the specified table.
  2. Step 2: Analyze console.log output

    If the item with UserId '123' exists, response.Item contains it; otherwise, it's undefined.
  3. Final Answer:

    Prints the item with UserId '123' if it exists, otherwise undefined. -> Option C
  4. Quick Check:

    GetItem returns one item or undefined = A [OK]
Quick Trick: GetItem fetches one item by key, prints it or undefined [OK]
Common Mistakes:
MISTAKES
  • Thinking it fetches all items
  • Confusing missing await with syntax error
  • Expecting a list instead of single item

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes