Reputation: 15760
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
},
Upvotes: 0
Views: 1189
Reputation: 48476
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);
}
},
Upvotes: 2