Min*_*ngo 21 javascript amazon-web-services node.js amazon-dynamodb aws-sdk-js
我正在尝试使用有关 aws 和 dynamo db 的 async/await 功能。下面是一个如何在异步等待前放置对象的示例,正如您在回调中看到的,您可以访问包含放置对象的数据。但是,在使用 async 和 promise 的第二个代码块中,结果是一个空对象,有什么想法吗?
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));
}
});
Run Code Online (Sandbox Code Playgroud)
Promise async Version - 假设包装函数被标记为 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)
Run Code Online (Sandbox Code Playgroud)
当您使用 Promise 时,您应该使用 .then() 和 .catch() 处理返回的 Promise 对象。如果您查看文档,您的请求应如下所示:
dynamoDb.put(params).promise()
.then(function(data) {
console.log(data);
})
.catch(function(err) {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
这也将帮助您查看是否遇到任何错误(与使用 try/catch 围绕 await 的想法相同,但语法更清晰)
| 归档时间: |
|
| 查看次数: |
18030 次 |
| 最近记录: |