Search code examples
javascriptamazon-web-servicesamazon-dynamodbserverless

Why TypeError: dynamodb.deleteItem is not a function?


I am making aws serverless application with classic crud operations. Lambdas get and update works properly. But I have a issues with delete...

    const key = event.pathParameters.id;
    const dynamodb = new AWS.DynamoDB.DocumentClient();
    const params = {
      TableName: process.env.DYNAMODB_CUSTOMER_TABLE,
      Key: {
        primaryKey: key,
      },
    };

    const result = await dynamodb.deleteItem(params).promise();

It fails with "TypeError: dynamodb.deleteItem is not a function".

dynamodb initialization same as in working functions and I can print it. I provided proper rights: - 'dynamodb:DeleteItem'

What I did wrong?


Solution

  • You are initializing a document client here:

    const dynamodb = new AWS.DynamoDB.DocumentClient();
    

    Document client has a delete function. From the docs:

    delete(params, callback) ⇒ AWS.Request: Deletes a single item in a table by primary key by delegating to AWS.DynamoDB.deleteItem()

    If you want to have an actual deleteItem function, you would want to initialize your DynamoDB client as follows:

    const dynamodb = new AWS.DynamoDB();
    // ...
    const result = await dynamodb.deleteItem(params).promise();