Search code examples
aws-lambdaamazon-dynamodb

dynamo.updateItem(...).promise is not a function


I'm referring to the CDK tutorial here https://cdkworkshop.com/20-typescript/40-hit-counter/200-handler.html

but my code below

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

will return this error dynamo.updateItem(...).promise is not a function. It seems the tutorial is outdated.. what should I change to fix the code?


Solution

  • It's outdated yes, Lambdas runtime now includes JavaScript SDk V3 but your coding for V2.

    If you're using the V3 JavaScript SDK then the updateItem returns a promise by dead.

    Here's an example

    const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
    const { DynamoDBDocumentClient, UpdateCommand } = require("@aws-sdk/lib-dynamodb");
    
    ...
    ...
    
    
    await ddbDocClient.send(
          new UpdateCommand({
            TableName: "RetailDatabase",
            Key: {
              pk: "jim.bob@somewhere.com",    // Partition key
              sk: "metadata",                 // Sort key
            },
            ExpressionAttributeNames: {
              "#n": "name",
            },
            UpdateExpression: "set #n = :nm",
            ExpressionAttributeValues: {
              ":nm": "Big Jim Bob",
            },
            ReturnValues: "ALL_NEW",
          })
      );