Created
August 14, 2011 14:12
-
-
Save mikekunze/1144904 to your computer and use it in GitHub Desktop.
Relational MongoDB with async foreach library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/vendors.json', function(req, res) { | |
var session = req.session; | |
var query = mods.url.parse(req.url,true).query; | |
var p_vendors = mods.mongoose.model('p_vendors'); | |
var vendors = mods.mongoose.model('vendors'); | |
var results = []; | |
p_vendors.find({ account_id: session.account_id }, function(err, docs) { | |
var async = mods.async; | |
var pushDoc = function(item, callback) { | |
if(item) { | |
vendors.findOne({ _id: item.vendor_id, description: new RegExp(query.query, 'i')}, function(err, vendor) { | |
if(vendor != null) { | |
results.push(vendor); | |
callback(); | |
} else callback(); | |
}); | |
} | |
}; | |
async.forEach(docs, pushDoc , function(err) { | |
if(err) | |
console.log(err); | |
var response = { | |
success: true, | |
results: results.length, | |
items: results | |
} | |
res.send(response); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment