0

I have a function with callback, where I'm using "listTables" method of dynamoDB, which returns just 100 table names and if there is anymore tables, it returns another field called "LastEvaluatedTableName" which we can use in our new query in listTables to query another 100 tables from the mentioned "LastEvaluatedTableName"; how can I have recursion in callbacks in javascript in this logic?

I have tried the following which is not correct:

module.exports.ListTables = function (start, callback) {
    var params;
    if (start) {
        params = {
            "ExclusiveStartTableName": start
        };
    }

    dynamodb.listTables(params, function (err, data) {
        var totalData = [];
        totalData.push(data);
        if (data.LastEvaluatedTableName) {
            data = module.exports.ListTables(data.LastEvaluatedTableName);
        }
        callback(err, totalData);

    });
}

Please let me know if you need more clarifications!

Thanks!

1 Answer 1

1

You need to concat your data, not replace it each time:

dynamodb.listTables(params, function (err, data) {
    if (data.LastEvaluatedTableName) {
        data.concat(module.exports.ListTables(data.LastEvaluatedTableName));
    }
    callback(err, data);
});

UPDATE

Based on the info from the comment, sounds like you need something like this:

module.exports.ListTables = function (start, callback, totalData) {
    var params;
    if (start) {
        params = {
            "ExclusiveStartTableName": start
        };
    }

    if (!totalData) {
        totalData = [];
    }

    dynamodb.listTables(params, function (err, data) {
        totalData = totalData.concat(data.TableNames);
        if (data.LastEvaluatedTableName) {
            module.exports.ListTables(data.LastEvaluatedTableName, callback, totalData);
        }
        else {
            callback(err, totalData);
        }
    });
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thansk so much man :-) Actuatly the result of ListTables from Dynamo is like : { LastEvaluatedTableName: 'test100', TableNames: [ 'test1',...]} and it seems it is not appending the results!
You're welcome! Oh, I see, you know how to append the right stuff?
it is an object so it gives ""Object #<Object> has no method 'concat'" so I tried data.TableNames.concat(module.exports.ListTables(data.LastEvaluatedTableName).TableNames); and this gives "Cannot read property 'TableNames' of undefined"
The logic seems right, but still "Cannot read property 'TableNames' of undefined"...
Damn, 3AM... forgot to assign the concatted data: totalData = totalData.concat(data.TableNames);. The case with no recursion should also work fine with that code.
|

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.