I have a function that gets some data from DynamoDB using docClient.query(). I'm able to retrieve data and print it on console e.g., console.log(data)), but if I try to return data I always get undefined.
I thought that function (err,data){ ... } was a callback of the query() function and was hoping it would wait for the value to be available to before returning.
Clearly I'm new with AWS SDK and async functions, couldn't find documentation that used return in the way I needed.
I just need the aliasHasRole to return the isAuthorized JSON so I can use it elsewhere outside the function.
function aliasHasRole(an_alias, a_role) {
const params = {
TableName: 'xxxxxxx',
KeyConditionExpression: '#alias= :alias AND #Role= :Role',
ExpressionAttributeNames: {
'#alias': 'alias',
'#Role': 'Role'
},
ExpressionAttributeValues: {
':alias': an_alias,
':Role': a_role,
},
};
docClient.query(params, function (err, data) {
if (err) {
console.log("Error when attempting table query, see below:\n\n" +
JSON.stringify(err, null, 2));
return err;
} else {
var isAuthorized = data.Count === 1 && data.Items[0].alias === an_alias && data.Items[0].Role === a_role ? true : false;
console.log(1,'Access', isAuthorized ? 'Granted' : 'Denied', 'for alias "' + an_alias + '".\n');
return isAuthorized; //always returns undefined
}
})
}
console.log(aliasHasRole("fooAlias","barRole")) // returns undefined.