4

i have a silly problem that is driving me crazy. I have a AWS lambda function that has a call to Dynamo db.

i want to have the ddb.Scan operation into a function get the result and then pass it to the event, but i cannot return the result inside the function...

var aws = require('aws-sdk');
var ddb = new aws.DynamoDB();

function getName(userid) {

 ddb.scan({
        TableName: "Users",
        ScanFilter: {
            "userid":
                    {
                        "AttributeValueList": [
                            {"S": userid}
                        ],
                        "ComparisonOperator": "EQ"
                    }
        }
    }, function (err, data) {
        return data.Items[0].username;
    });

};

exports.handler = function (event, context) {

        var userid= '4vwe6jd56es59q';

        var username = getName(userid);

        context.succeed({success: true, username: username}); 


};

can someone help me understanding where i get lost?

2 Answers 2

1

Notice how you declare a function here:

function getName(userid) {

And another function here:

function (err, data) {

It's that second function in which you are trying to return something, but nothing is expecting that function to return anything, so the return value is thrown away.

Note that the call to ddb.scan() returns immediately, and the anonymous callback function you passed to ddb.scan() gets called at some later time, after the DynamoDB response has been retrieved. This is a basic asynchronous programming concept that you will have to understand before you can successfully write NodeJS code. You will either need to pass a callback to your getName function, which it can call once it has the name value, or you might need to rethink the way you are designing your Lambda function entirely.

I would also recommend using the latest version of NodeJS available on AWS Lambda so that you can use promises instead of callbacks, which makes working with asynchronous functions a bit easier.

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

2 Comments

Thank you very much. the idea was to have it not async... I was following the basic query as link with the query based on "docClient.scan(params, function(err, data) {". Probably i'm totally on the wrong path but this is part of a bigger code where i have a main query on a table and i want to merge some informations from another table before to build the JSON...
That docClient.scan function is also asynchronous. You are going to have to deal with the asynchronous nature of these AWS API calls somehow if you want to write your code in NodeJS. The idea might be to "have it not async" but that's not an option the AWS NodeJS SDK gives you, and not really the model you use for writing NodeJS code anyway.
0

The first thing I noticed with your code is that your getName function doesn't actually return anything, so it's returning null even if the scan function works as intended.

More importantly, ddb.scan(...) is an async function, so therefore, you have to embed everything you want to be done with the result (in this case, the results of scan) inside of the callback.

That means your code should look more like this:

var aws = require('aws-sdk');
var ddb = new aws.DynamoDB();

exports.handler = function (event, context) {
        var userid= '4vwe6jd56es59q';
        var username = getName(userid);

  ddb.scan({
        TableName: "Users",
        ScanFilter: {
            "userid":
                    {
                        "AttributeValueList": [
                            {"S": userid}
                        ],
                        "ComparisonOperator": "EQ"
                    }
        }
    }, function (err, data) {
        var username = data.Items[0].username
        context.succeed({success: true, username: username});   
    });
};

Note the context.succeed is now embedded inside of the async callback, so that the data is passed only when the data has been returned.

1 Comment

Thanks Michael, But I was trying to run it on the other way because that's a part of other code. into the exports. handler i have a foreach and i need to run the query into the for each in order to add the result of this query to the JSON... I have a first query that is as you are suggesting and for each data.Item in the result I need to nest a second query.

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.