aws-samples/aws-cdk-intro-workshop

TypeError when updating item to DynamoDB in hitcounter.js

HAoRAn-W opened this issue · 1 comments

Describe the bug

I was following the steps on Hit counter Lambda handler, and got a TypeError in the following code:

  // update dynamo entry for "path" with hits++
  await dynamo.updateItem({
    TableName: process.env.HITS_TABLE_NAME,
    Key: { path: { S: event.path } },
    UpdateExpression: 'ADD hits :incr',
    ExpressionAttributeValues: { ':incr': { N: '1' } }
  }).promise();

Expected Behavior

The code should cause an AccessDeniedException according to the tutorial.

Current Behavior

Error message on CloudWatch log is caused by dynamo.updateItem(...).promise

{
    "errorType": "TypeError",
    "errorMessage": "dynamo.updateItem(...).promise is not a function",
    "stack": [
        "TypeError: dynamo.updateItem(...).promise is not a function",
        "    at exports.handler (/var/task/hitcounter.js:17:6)",
        "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1085:29)"
    ]
}

Reproduction Steps

Node.js version: v18.12.1
I used

const {Lambda} = require("@aws-sdk/client-lambda");
const {DynamoDB} = require("@aws-sdk/client-dynamodb");

instead of

const { DynamoDB, Lambda } = require('aws-sdk');

because using aws-sdk will cause Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'

The rest of the code is not changed.

Possible Solution

Additional Information/Context

No response

CDK CLI Version

2.69.0 (build 60a5b2a)

Section

No response

Browser

No response

Language

TypeScript

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.