1

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

1 Answer 1

3

You are not waiting for the update to DynamoDB to finish.

Update it to return a promise and use async/await

async function sentimentVal(inputparams) {
    try {
     //    function resultrequest() 
     const data = await ddb.get(inputparams).promise()

     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 ;
   } catch (err) {
      console.log("Error", err);
      throw err
   }
}

And await for it in handler

exports.handler = (event) => {
    // Read options from the event.

    var paramsNeutral = {
        TableName: 'twitterSentiment',
        Key: { 'sentiment':'NEUTRAL' }
    };

    // Call sentimentVal function with paramsNeutral, and setNeutralValue callback function
    // 
    const tweet = await sentimentVal(paramsNeutral, setNeutralValue);

    function setNeutralValue (error, tweetValue) {
        if (error) console.error('ERROR !', error) ;
        else console.log ('callback tweetValue = ', tweetValue) ;
    }
};

I'm not sure what setNeutralValue is supposed to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thank you for your help ! I tried to use your corrections but I have an error on " const tweet = await sentimentVal(paramsNeutral, setNeutralValue);" line It shows an error "Parsing error: Unexpected token sentimentVal". Do you have an idea on what may cause this ? Thank you !
I succedeed adding async in exports.handler = async (event) => {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.