Ad

How To Fix This Is Not A Function Nodejs

I am following this article from medium https://blog.bitsrc.io/serverless-backend-using-aws-lambda-hands-on-guide-31806ceb735e

Everything works except when I attempt to add a record to the DynamoDB I get an error that say "this is not a function"

const AWS = require ("aws-sdk");

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

const uuid = require ("uuid");

module.exports.myHero = async (event) => {

    const data = JSON.parse(event.body);

    const params = {

        TableName: "myHeros",
        Item: {

            id: uuid(),
            name: data.name,
            checked: false
        }
    };

    await client.put(params).promise();

    return {

        statusCode: 200,
        body: JSON.stringify(data)
    };
};
{
    "errorMessage": "client.put(...).promise is not a function",
    "errorType": "TypeError",
    "stackTrace": [
        "module.exports.myHero (/var/task/create.js:30:27)"
    ]
}
Ad

Answer

In almost all cases, when you call a method xyz() on an AWS client object and it fails with ‘xyz is not a function’, the problem is that you are using an old version of an SDK that does not actually support that method.

Upgrading to the latest AWS SDK version will fix this problem.

Ad
source: stackoverflow.com
Ad