Nodejs Mongoose从集合中渲染两个模型 [英] Nodejs Mongoose render two models from collections

查看:259
本文介绍了Nodejs Mongoose从集合中渲染两个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望标题正确地描述我的问题.

I hope the heading describes my question right.

当我打开/admin/liste时,它应该呈现MongoDB数据库中的两个表. 如何渲染两个或更多集合?

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
        });
    });*/
});

我在参考文献中找不到关于我的问题的任何信息,该问题是重复的.我没有使用任何Joins之类的东西. 我想显示来自List1和List2中的项目的两个表.未注释的代码运行良好,但是这只是一个表,因此我必须将这两个表结合起来,然后呈现页面.

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.

推荐答案

感谢您的回答.同时,我为我的问题找到了另一种解决方案:

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);
    });
});

这篇关于Nodejs Mongoose从集合中渲染两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆