如何解决这不是函数nodejs

How to fix this is not a function nodejs

我正在从媒体关注这篇文章 https://blog.bitsrc.io/serverless-backend-using-aws-lambda-hands-on-guide-31806ceb735e

一切正常,除了当我尝试向 DynamoDB 添加记录时,我收到一条错误消息 "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)"
    ]
}

当初始化 dynamodb 客户端时 new AWS.DynamoDB.DocumentClient()' 请将选项(至少区域参数)传递给 DocumentClient 函数。

在几乎所有情况下,当您在 AWS 客户端对象上调用方法 xyz() 并且它因“xyz 不是函数”而失败时,问题在于您使用的是旧版本的 SDK实际上不支持该方法。

升级到最新的 AWS SDK 版本将解决这个问题。