How to return multiple Mongodb collections in one app.get request?
I need to load two datasets from two different mongodb collections into one page.
My route page that makes a request via mongoose looks like this:
app.get('/testPage', function(req,res){
dbReadOne.find({}, '', function(error, dataOne){
res.json(dataOne);
});
dbReadTwo.find({},'', function(error, dataTwo){
res.json(dataTwo);
});
});
My Angular factories look like this
app.factory('dataOneFactory', function($resource){
return $resource('testPage/:dataOne', {}, {
query: { method: 'GET', params: {symbol: 'dataOne'}, isArray: true}
})
});
app.factory('dataTwoFactory', function($resource){
return $resource('testPage/:dataTwo', {}, {
query: { method: 'GET', params: {customList: 'dataTwo'}, isArray: true}
})
});
I am completely lost on how to do this. Any advice I can get on this matter would be grateful. Thank you.
+3
source to share
1 answer
Since Node.js is asynchronous in nature, the dbReadOne.find function runs asynchronously, so you must call the next dbReadOne.find in the callback of the first dbReadOne.find function.
Example:
app.get('/testPage', function(req,res){
dbReadOne.find({}, '', function(error, dataOne){
dbReadTwo.find({},'', function(error, dataTwo){
res.json({
dataOne: dataOne,
dataTwo: dataTwo
});
});
});
});
+1
source to share