Nodejs Mongoose render two models from collections

时光总嘲笑我的痴心妄想 提交于 2019-12-08 09:37:09

问题


I hope the heading describes my question right.

When I open /admin/liste, it should render two tables from MongoDB database. How can I render two or more collections?

app.get('/admin/liste', isLoggedIn, function(req, res) {
    var List1 = mongoose.model('List1');
   // var List2 = mongoose.model('List2');

    List1.find(function (err, docs) {
        res.render('admin/liste',{
            firstlist : docs
        });
    });

    /*List2.find(function (err, docs) {
        res.render('admin/liste',{
            seclist : docs
        });
    });*/
});

EDIT: I can't find any information to my problem in the reference, that this question is dublicate. I'm not using any Joins or something like that. I want to display two tables from the items which are in List1 and List2. The uncommented out code is working well, but this is only one table, so I have to combine those two, then render the page.

Hope anyone can help me, thank you.


回答1:


You can do this by executing the second query within the callback of the first query so that both results are available for the res.render call:

app.get('/admin/liste', isLoggedIn, function(req, res) {
    var List1 = mongoose.model('List1');
    var List2 = mongoose.model('List2');

    List1.find(function (err, docs1) {
        List2.find(function (err, docs2) {
            res.render('admin/liste', {
                firstlist : docs1
                seclist : docs2
            });
        });
    });
});



回答2:


Thank you for your answer. Meanwhile I found another solution for my problem:

app.get('/admin/liste', isLoggedIn, function(req, res) {
    var List1 = mongoose.model('List1');
    var List2 = mongoose.model('List2');

    var List1Objects = List1.find({});
    var List2Objects = List2.find({});
    var resources = {
        firstlist: List1Objects.exec.bind(List1Objects),
        seclist: List2Objects.exec.bind(List2Objects)
    };

    async.parallel(resources, function (error, results){
        if (error) {
            res.status(500).send(error);
            return;
        }
        res.render('admin/liste', results);
    });
});



回答3:


I think we should first find items in List1 and store them in a variable. Then find items in List2 and render the ejs. Something like this:

exports.someFunction = async (req, res) => {
   let List1 = mongoose.model('List1');
   let List2 = mongoose.model('List2');

   let itemsInList1 = await List1.find(); // store items in List1 in variable

   List2.find().then(itemsInList2 => {
      res.render('admin/liste', {
         firstlist : itemsInList1,
         secondList: itemsInList2 
      });
   })
}



回答4:


This works for me:

app.get('/admin/liste', function (req, res) {
  Registers.find({}, function (req1, res1) {
     Registers.find({}, function (req2, res2) {
        res.render('liste', {firstlist: res1, firstlist: res2});
    })
});

});



来源:https://stackoverflow.com/questions/26402781/nodejs-mongoose-render-two-models-from-collections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!