Bird
0
0

```javascript const AWS = require('aws-sdk'); const docClient = new AWS.DynamoDB.DocumentClient(); exports.handler = async (event) => { const params = { TableName: 'Users', Key: { id: '101' } }; const data = await docClient.get(params).promise(); return data.Item ?

medium📝 query result Q4 of 15
DynamoDB - with Serverless
Given this Lambda function snippet, what will be the output if the item with id '101' exists in the DynamoDB table? ```javascript const AWS = require('aws-sdk'); const docClient = new AWS.DynamoDB.DocumentClient(); exports.handler = async (event) => { const params = { TableName: 'Users', Key: { id: '101' } }; const data = await docClient.get(params).promise(); return data.Item ? data.Item.name : 'Not Found'; }; ``` Assuming the item with id '101' has name 'Alice'.
A"Alice"
B"Not Found"
CAn error is thrown
D"undefined"
Step-by-Step Solution
Solution:
  1. Step 1: Understand the get operation

    The get method fetches the item with id '101' from the 'Users' table.
  2. Step 2: Check the return logic

    If data.Item exists, it returns data.Item.name, which is 'Alice'.
  3. Final Answer:

    "Alice" -> Option A
  4. Quick Check:

    Item found returns name = "Alice" [OK]
Quick Trick: get returns Item; check if exists before accessing properties [OK]
Common Mistakes:
MISTAKES
  • Assuming 'Not Found' when item exists
  • Expecting error without cause
  • Returning undefined instead of name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes