简体   繁体   English

如何在一个app.get请求中返回多个Mongodb集合?

[英]How to return multiple Mongodb Collections in one app.get request?

I need to load two sets of data from two different mongodb collections onto one page. 我需要从两个不同的mongodb集合中将两组数据加载到一个页面上。
My route page that makes the request through mongoose looks like this 通过mongoose发出请求的路由页面如下所示

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 我的Angular工厂看起来像这样

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'm completely lost on how to do this. 我完全迷失了如何做到这一点。 I'd appreciate any advice I can get on this issue. 我很感激我能就这个问题提出任何建议。 Thanks. 谢谢。

Since Node.js is asynchronous in nature, dbReadOne.find function runs asynchronously, therefore you should call the next dbReadOne.find in the callback of first dbReadOne.find function. 由于Node.js的在本质上是异步的,dbReadOne.find功能异步运行,因此,你应该叫下一个dbReadOne.find在第一dbReadOne.find功能的回调。

Example: 例:

app.get('/testPage', function(req, res){
     dbReadOne.find({}, '', function(errorOne, dataOne){
         if(errorOne)
            throw new Error(errorOne);
         dbReadTwo.find({},'', function(errorTwo, dataTwo){
            if(errorTwo)
                throw new Error(errorTwo);
            res.json({
                dataOne: dataOne,
                dataTwo: dataTwo
            });
         });
     });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM