I'm trying to create a Lambda function to generate a .js script (in order to use with Chart.JS).
This script sends a query to a table in DynamoDB and outputs the results in .js file (which is stored in an S3 bucket).
I try for many hours to make it functional, but I'm stuck with classical problems on Node.js: order on callback functions and variables scope.
Here is the code I used:
var AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-1'});
var s3 = new AWS.S3();
var tweetValue ;
var neutralValue ;
var destBucket = "twitterappfront1";
var ddb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
function sentimentVal(inputparams) {
// function resultrequest()
ddb.get(inputparams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Item);
//Catch tweets number in DynamoTB table and store un descriptor
var numtweets = (JSON.parse(JSON.stringify(AWS.DynamoDB.Converter.marshall(data.Item)))).tweets ;
var tweetsObject = Object.getOwnPropertyDescriptor(numtweets, 'N') ;
tweetValue = tweetsObject.value ;
console.log ("test stringify = ", numtweets) ;
console.log (tweetsObject.value) ;
console.log ("Value = ", tweetValue) ;
return tweetValue ;
}
});
}
exports.handler = (event) => {
// Read options from the event.
var paramsNeutral = {
TableName: 'twitterSentiment',
Key: { 'sentiment':'NEUTRAL' }
};
// Call sentimentVal function with paramsNeutral, and setNeutralValue callback function
//
sentimentVal(paramsNeutral, setNeutralValue);
function setNeutralValue (error, tweetValue) {
if (error) console.error('ERROR !', error) ;
else console.log ('callback tweetValue = ', tweetValue) ;
}
};
My problem is that it seems the callback function is never used: I have no console output "ERROR" or "Callback tweetValue ="
And I don't understand how to catch the value from the sentvimentVal function. I tried a "return", but I don't know if it is the right way.
Can you please help me ?
Thank you