'callback.call is not a function at Request

I have written an AWS Lambda function using NodeJS and trying to rename a file or object on AWS S3 however I'm getting this error:-

2022-03-09T09:42:57.696+05:30

Copy
2022-03-09T04:12:57.695Z    382f60f2-7c29-48f4-a542-d2a3c9a27844    INFO    Error [TypeError]: callback.call is not a function
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:367:18)
    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:686:14)
    at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:688:12)
    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:116:18) {
  code: 'TypeError',
  time: 2022-03-09T04:12:57.694Z
}

Code:-

const AWS = require('aws-sdk');    
const s3 = new AWS.S3({ apiVersion: '2006-03-01' });    
const moment = require('moment');    
const logger = require('./logger').logger    
const s3DataBucket = process.env.RUNTIME_S3_DATA_BUCKET;

exports.lambdaHandler = async (event) => {
  let statusCode = 200;
  const headers = {
    'Access-Control-Allow-Headers': '*',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': '*'
  };

  if (!event && !event.body) {
    throw new Error('No payload has been received');
  }
  const body = JSON.parse(event.body);

  if (!body.user.id || !body.user.file) {
    throw new Error('Invalid payload');
  }

  let userId = `${body.user.id}`;
  let file = JSON.parse(JSON.stringify(body.user.file));
  let oldKey = `${userId}/${moment(file.date).format("YYYY-MM-DD")}/${file.oldName}`;
  let newKey = `${userId}/${moment(file.date).format("YYYY-MM-DD")}/${file.newName}`;
  let data;
  var params = {
    Bucket: s3DataBucket,
    CopySource: oldKey,
    Key: newKey
  };
  try {
    console.log("params::", params); //Params not in Use
    s3.copyObject(s3DataBucket, oldKey, s3DataBucket, newKey);
    console.log("done......");
    data = 'it is done';
  } catch (e) {
    console.log(e);
    return {
      statusCode: 400,
      headers
    }
  } finally {
    return {
      statusCode,
      body: JSON.stringify(data),
      headers
    }
  }
};

I tried modifying the code. I can see the value for params correctly printed on CloudWatch but nothing worked. Would really appreciate it if someone can tell me the issue behind it, thanks

[New working code]

    var copyparams = {
    Bucket: `${s3DataBucket}`,
    CopySource: `/${s3DataBucket}/${oldKey}`,
    Key: `${newKey}`
  };
  const deleteparams = {
    Bucket: `${s3DataBucket}`,
    Key: `${oldKey}`
  };
  try {
    console.log("copyparams::", copyparams);
    console.log("renaming object now....");
    await s3.copyObject(copyparams).promise();

    console.log("deleting object now....");
    console.log("deleteparams::", deleteparams);
    await s3.deleteObject(deleteparams).promise();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source