this seems like a very simple operation and I'm sure I will kick myself when it is solved!
This function retrieves a date from a Dynamodb database from AWS Lambda running Node.js 6.10. Its called elsewhere as
var lastLogin = getLastLogin(UserId);
Looking at the console it is successfully retrieving the date and i get "obj is 2018-04-08" but I just cant seem to pass the "obj" string into the lastLogin variable.
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
};