【发布时间】:2021-04-16 00:42:37
【问题描述】:
这似乎是一个非常简单的操作,我相信当它解决后我会踢自己!
此函数从运行 Node.js 6.10 的 AWS Lambda 的 Dynamodb 数据库中检索日期。它在其他地方称为
var lastLogin = getLastLogin(UserId);
查看控制台,它成功检索日期,我得到“obj is 2018-04-08”,但我似乎无法将“obj”字符串传递到 lastLogin 变量中。
function getLastLogin(UserId) {
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "REMOVED-FOR-PRIVACY";
var params = {
TableName:table,
Key:{
"UserId": UserId,
}
};
console.log("Getting last login...");
docClient.get(params, ((err, data) => {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
var payload = JSON.stringify(data, null, 2);
console.log ('payload content = ' + payload);
if (payload == '{}') {
console.log ('found nothing');
} else {
obj = JSON.parse (payload).Item.lastLogin;
obj = JSON.stringify(obj);
console.log ("obj is " + obj); // obj is 2018-04-08
}}
return obj; }));
console.log("Returned date is " + obj); // Returned date is undefined
};
【问题讨论】:
标签: node.js amazon-web-services aws-lambda amazon-dynamodb