dynamodb.put().promise() 不返回 put 对象 [英] dynamodb.put().promise() not returning the put object

查看:35
本文介绍了dynamodb.put().promise() 不返回 put 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 async/await 与 aws 和 dynamo db 相关的功能.下面是一个如何在异步等待之前放置对象的示例,正​​如您在回调中看到的那样,您可以访问包含放置对象的数据.然而,在使用 async 和 promise 的第二个代码块中,结果是一个空对象,有什么想法吗?

I am trying to make use of the async/await functionality with regard to aws and dynamo db. Below is an example of how to put an object pre asyn await, as you can see in the callback you have access to data which contains the put object. However in the second block of code which uses async and promise the result is an empty object, any thoughts?

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html

非承诺版本

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("Adding a new item...");
docClient.put(params, function(err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});

Promise 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) 

推荐答案

根据doc 如果你想要返回一些东西,你必须使用 ReturnValues.

According to the doc you have to use ReturnValues if you want something back.

这篇关于dynamodb.put().promise() 不返回 put 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆