0

Folks, Trying to wrap my brain around the following problem. I have an input array coming in via http.POST, which I would like to loop through, fire off multiple dynamodb queries, assemble an output, then return this output as one JSON object.

I've verified that the POST is working fine, array is built properly, all I need to do is have several DynamoDB calls fired off, assemble each result, then return it.

Since this is async, how would I write the following?

allVehicles: function (req, res, next) {

    checkDB(req, res, next)

    function checkDB(req, res, next) {

        async.each(req.body, lookupDB, function(err) {
            console.log("wtf err");
        });

        returnResponse(callback)
    }

    function lookupDB(uID) {
        var checkUsers = {
            TableName : 'tablename',
            IndexName : 'license-index',
            KeyConditions : 
            {
                "entry":
                {
                    "AttributeValueList" : [
                    {
                        "S" : '1'
                    }
                    ],
                    "ComparisonOperator" : "EQ"
                },
                "license" : 
                {
                    "AttributeValueList" : [
                    {
                        "S" : uID
                    }
                    ],
                    "ComparisonOperator" : "EQ"
                }
            }
        }//var
        db.query(checkUsers, function(err, data) {
            if (err) {
                console.log(err)
            } else {
                if (data.Count > 0) {
                    console.log("found ", data.Count")
                }
            }
        });//dbq
    }

    function returnResponse(upstreamData) {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
        res.send(JSON.stringify(upstreamData, undefined, 2));
        res.end();
    }//fun
},
0

1 Answer 1

2

Try async.map.

allVehicles: function (req, res, next) {

    async.map(req.body, lookup, function(err, results) {
        if (err) {
            return next(err);
        }
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
        res.send(JSON.stringify(results, undefined, 2));
        res.end();
    });

    function lookup(id, done) {

        db.query({
            TableName: 'tablename',
            IndexName: 'license-index',
            KeyConditions: {
                entry: {
                    AttributeValueList: [{ S: '1' }],
                    ComparisonOperator: 'EQ'
                },
                license: {
                    AttributeValueList: [{ S: id }],
                    ComparisonOperator: 'EQ'
                }
            }
        }, done);

    }

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

1 Comment

came back to your answer after a few months. Thanks again!

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.