使用Express将多个数据库查询结果发送到单个视图 [英] Send multiple DB query results to a single view using Express

查看:86
本文介绍了使用Express将多个数据库查询结果发送到单个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仪表板视图(dashboard.jade),它将显示两个面板,这些面板具有不同的信息,所有这些信息都应从数据库中检索,然后发送到该视图.

I have a dashboard view ( dashboard.jade ) that will display two panels with different information, all that info should be retrieved from a database and then sent to the view.

假设我有一个路由文件(document.js),其中定义了两个操作:

Let's say i have a route file ( document.js ) with two actions defined:

exports.getAllDocuments = function(req, res){
    doc = db.model('documents', docSchema);

    doc.find({}, function(err, documents) {
        if (!err) { 
            // handle success
        }
        else { 
            throw err;
        }
    });
};

exports.getLatestDocumentTags = function(req, res){
    tags = db.model('tags', tagSchema);

    tags.find({}, function(err, docs) {
        if (!err) { 
            // handle success
        }
        else { 
            throw err;
        }
    });
};

这些功能仅用于从数据库检索数据的目的.

These functions would only serve the porpuse of retrieving data from the database.

现在,我想将数据从我在呈现仪表板视图的exports.index函数下的dashboard.js路由文件中发送到仪表板视图.

Now i would like to send that data to the dashboard view from my dashboard.js route file under exports.index function where i render my dashboard view.

问题是,由于数据库调用将是异步的,因此在调用视图之前,我将无法访问数据.

The problem is, since the db calls will be async i wouldn't have access to the data before i could call the view.

我想我可以执行一个简单的操作,即执行所有数据库调用并通过回调一次将所有数据传递到视图,但这会使我的数据检索操作不可重用.

I guess i could have an action that simply did all my db calls and through callbacks deliver all the data at once to the view but that would make my data retrieval actions not reusable.

我对如何正确解决此问题感到非常困惑,可能是我把所有异步问题都弄错了.有人可以给我一些如何正确执行操作的提示吗?

I'm really confused on how to tackle this problem correctly, probably i'm getting this async thing all wrong. Can someone give me some hints on how to do this properly ?

推荐答案

这里有一些引起您兴趣的东西.

Here's something to pique your interest.

//Check out the async.js library
var async = require('async');

//Set up your models once at program startup, not on each request
//Ideall these would be in separate modules as wel
var Doc = db.model('documents', docSchema);
var Tags = db.model('tags', tagSchema);

function index(req, res, next) {
    async.parallel({ //Run every function in this object in parallel
    allDocs: async.apply(Doc.find, {}) //gets all documents. async.apply will
    //do the equivalent of Doc.find({}, callback) here
    latestDocs: async.apply(Tags.find, {})
    ], function (error, results) { //This function gets called when all parallel jobs are done
      //results will be like {
      //  allDocs: [doc1, doc2]
      //  latestDocs: [doc3, doc4]
      // }
      res.render('index', results);
    });
}
exports.index = index;
};

尝试更多教程.如果您还没有关于异步编程如何在节点上工作的哈哈"时刻,请在尝试编写无指导的全新程序之前,继续阅读有指导性的手持教程.

Try some more tutorials. If you haven't had the "a ha" moment about how async programming works in node, keep going through guided, hand-held tutorials before trying to write brand new programs without guidance.

这篇关于使用Express将多个数据库查询结果发送到单个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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