dynamodb.put().promise() 不返回放置对象
dynamodb.put().promise() not returning the put object
我正在尝试使用与 aws 和 dynamo db 有关的 async/await 功能。下面是一个如何在异步等待前放置对象的示例,正如您在回调中看到的那样,您可以访问包含放置对象的数据。但是在第二个代码块中使用异步并承诺结果是一个空对象,有什么想法吗?
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 异步版本 - 假设包装函数被标记为异步
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)
当您使用 promise 时,您应该使用 .then() 和 .catch() 处理返回的 promise 对象。 If you take a look at the documentation,您的请求应如下所示:
dynamoDb.put(params).promise()
.then(function(data) {
console.log(data);
})
.catch(function(err) {
console.log(err);
});
这也将帮助您查看是否遇到任何错误(与使用 try/catch 包围 await 的想法相同,但语法更清晰)
根据 doc 如果你想要回一些东西,你必须使用 ReturnValues
。
我试过了ReturnValues: 'ALL_OLD'
但在 async await
中没有结果。
这是部分代码:
const answersParams = {
TableName: ANSWERS_TABLE,
Item: {
answersId,
answers,
userId,
quizId,
},
ReturnValues: 'ALL_OLD',
};
try {
const createdAnswres = await db.put(answersParams).promise();
return {
statusCode: 201,
body: JSON.stringify(createdAnswres && createdAnswres.Item),
};
} catch (error) {
return {
statusCode: 500,
body: 'failed to save user answers',
};
}
}
所以我不得不向数据库添加另一个请求:
const createdAnswres = await db.get({
TableName: ANSWERS_TABLE,
Key: { answersId },
})
.promise();
我正在尝试使用与 aws 和 dynamo db 有关的 async/await 功能。下面是一个如何在异步等待前放置对象的示例,正如您在回调中看到的那样,您可以访问包含放置对象的数据。但是在第二个代码块中使用异步并承诺结果是一个空对象,有什么想法吗?
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 异步版本 - 假设包装函数被标记为异步
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)
当您使用 promise 时,您应该使用 .then() 和 .catch() 处理返回的 promise 对象。 If you take a look at the documentation,您的请求应如下所示:
dynamoDb.put(params).promise()
.then(function(data) {
console.log(data);
})
.catch(function(err) {
console.log(err);
});
这也将帮助您查看是否遇到任何错误(与使用 try/catch 包围 await 的想法相同,但语法更清晰)
根据 doc 如果你想要回一些东西,你必须使用 ReturnValues
。
我试过了ReturnValues: 'ALL_OLD'
但在 async await
中没有结果。
这是部分代码:
const answersParams = {
TableName: ANSWERS_TABLE,
Item: {
answersId,
answers,
userId,
quizId,
},
ReturnValues: 'ALL_OLD',
};
try {
const createdAnswres = await db.put(answersParams).promise();
return {
statusCode: 201,
body: JSON.stringify(createdAnswres && createdAnswres.Item),
};
} catch (error) {
return {
statusCode: 500,
body: 'failed to save user answers',
};
}
}
所以我不得不向数据库添加另一个请求:
const createdAnswres = await db.get({
TableName: ANSWERS_TABLE,
Key: { answersId },
})
.promise();