I am working with Amazon DynamoDB and Express.
I need to render a view based on the data from a Amazon DynamoDB table.
My code works fine when I work with callback API instead of trying to use promise.
But I want to use promise because to keep my code clean, otherwise I need to call res.send() from inside the callback of the docClient.scan(params).
My code for working with promise as follows I can't figure out what is wrong;
async function test(params){
AWS.config.loadFromPath('./awsconfigtest.json');
let docClient = new AWS.DynamoDB.DocumentClient();
await docClient.scan(params).promise();
}
Below is the content of the route file;
/* GET home page. */
router.get('/', function(req, res, next) {
let scanResults ;
let params = {
TableName: 'dummy'
};
test(params).then((data,err)=>{
console.log(data,err);
data.Items.forEach(function (element, index, array) {
scanResults.push({name: element.name, nodeId: element.nodeId});
console.log(element.name + " (" + typeof element.nodeId + ")");
});
});
console.log(scanResults);
res.render("index",{nodes:scanResults});
});