节点Promisify TypeError [ERR_INVALID_ARG_TYPE]:“原始”参数必须是函数类型

时间:2019-09-16 11:21:14

标签: node.js amazon-web-services amazon-dynamodb

我有一个函数要使用节点的Util库进行赋值,
该功能用于将数据批量上传到AWS DynamoDb
我遇到以下错误-

TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type function

以下是代码-

let AWS = require('aws-sdk');
let util = require('util');

AWS.config.update({
    region: 'us-east-1'
});

let connection = new AWS.DynamoDB.DocumentClient();

let batchWrite = async () => {

    try {
        let params = {
            RequestItems: {
                "some_table": [
                {
                    PutRequest: {
                        Item: {
                            "url": {
                                S: "/admin/details"
                            },
                            "allowed_roles": {
                                "L" : [
                                    {
                                        S: "admin"
                                    },
                                    {
                                        S: "Test Lambda"
                                    },
                                    {
                                        S: "Test Lambda Role"
                                    }
                                ]
                            }
                        }
                    }
                }]
            }
        };

        let dynamoDbBatchWriteItem = util.promisify(connection.batchWriteItem).bind(connection);
        let response = await dynamoDbBatchWriteItem(params);
        console.log('batchWriteItem response => ', response);
    } catch (error) {
        console.log('error =>\n',error);
    }

};

错误在此行-

let dynamoDbBatchWriteItem = util.promisify(connection.batchWriteItem).bind(connection);

参考-
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchWriteItem-property

1 个答案:

答案 0 :(得分:0)

尝试一下:

const documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.batchWrite = util.promisify(documentClient.batchWrite);
result = await documentClient.batchWrite(params);