dynamodb.put().promise()不返回put对象。

9 浏览
0 Comments

dynamodb.put().promise()不返回put对象。

我正在尝试在AWS和DynamoDB中利用async/await功能。下面是一个在使用async/await之前如何put一个对象的示例,可以看到在回调函数中可以访问包含put对象的数据。然而,在使用async和promise的第二段代码中,结果是一个空对象,有什么想法吗?

非Promise版本:

var docClient = new AWS.DynamoDB.DocumentClient();
var table = "Movies";
var year = 2015;
var title = "The Big New Movie";
var params = {
    TableName:table,
    Item:{
        "year": year,
        "title": title,
        "info":{
            "plot": "Nothing happens at all.",
            "rating": 0
        }
    }
};
console.log("正在添加新项...");
docClient.put(params, function(err, data) {
    if (err) {
        console.error("无法添加项。错误JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("已添加项:", JSON.stringify(data, null, 2));
    }
});

Promise async版本 - 假设包装函数标记为async:

var docClient = new AWS.DynamoDB.DocumentClient();
var table = "Movies";
var year = 2015;
var title = "The Big New Movie";
var params = {
    TableName:table,
    Item:{
        "year": year,
        "title": title,
        "info":{
            "plot": "Nothing happens at all.",
            "rating": 0
        }
    }
};
const result: any = await dynamoDb.put(params).promise()
console.log(result) 

参考链接:[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html)

0