簡體   English   中英

使用玉石,表情和和尚渲染所有數據的視圖

[英]Rendering view with all the data using jade,express and monk

mongodb數據庫有兩個集合notes持有每個音符和notebook具有標題和數組notes包含arrayid指點下到notenotes集合。

//notes:
{
    "_id" : ObjectId("542afaaa4898f4042312872b"),
    "title" : "Hello Universe",
    "content" : "Saving to database"
}

//notebook
{
    "_id" : ObjectId("53d0c6d8daeac43cf8412945"),
    "title" : "My Notebook",
    "notes" : [
            ObjectId("53d0ca2fdaeac43cf8412946"),
            ObjectId("542afe34a25e3c44160e6b05"),
            ObjectId("542b7908a459766020342eae"),
            ObjectId("542b79efa459766020342eaf"),
            ObjectId("542b7a5ba459766020342eb0"),
            ObjectId("542b7aa3d3b135c022b674ff"),
            ObjectId("542bb47aa4ff29c820c22fdc"),
            ObjectId("542bb720a4ff29c820c22fdd"),
            ObjectId("542bddd5712c0dc426d41341"),
            ObjectId("542bdec5712c0dc426d41342")
    ]
}

我使用這些來檢索數據以呈現此jade視圖 。這是我的route的代碼:

   router.get('/main',function(req,res){
var db=req.db;
var notebookCollection=db.get('notebook');
var notesCollection=db.get('notes');
var locals={};
locals.title="TakeNote:An easy to use note taking app for web and hopefully mobile";
locals.sidebar={};
locals.sidebar.items=[];
//get data for the first notebook and display it by default
locals.notebook={};
locals.notebook.notes=[];
notebookCollection.find({},{},function(err,docs){
    if(err)
        console.error(err);
    else
    {
        console.log("Retrieving notebooks");
        var noteIds;
        if(docs.length)
        {
            console.log(docs)
            for(var i=0;i<docs.length;i++)
            {
                locals.sidebar.items.push(docs[i].title);
                locals.notebook.title=docs[i].title;
                noteIds=docs[i].notes;
                for(var j=0;j<noteIds.length;j++)
                {
                    notesCollection.findOne({_id:noteIds[j]},function(err,doc){
                        if(err)
                            console.error(err);
                        else
                        {
                            locals.notebook.notes.push(doc);
                            //res.render('main',locals);
                        }   
                    });
                    //putting it here might mean that it renders without any of the notes
                    //putting it here with a condition fails
                    /*
                    if(j===noteIds.length-1)
                        res.render('main',locals);
                    */
                }   
            }
            //putting it here means it renders without any of the notes 
        }   
    }   
});

});

我在哪里可以調用res.render('main',locals)以便它在此處呈現包含所有數據的模板?

使用異步時的EDIT錯誤:

使用async庫時出現此錯誤:

E:\Do\hello_express\node_notes\node_modules\async\lib\async.js:641
                    tasks[index].apply(null, arguments);
                                 ^
TypeError: Object #<Promise> has no method 'apply'
    at fn (E:\Do\hello_express\node_notes\node_modules\async\lib\async.js:641:34
)
    at Object._onImmediate (E:\Do\hello_express\node_notes\node_modules\async\li
b\async.js:557:34)
    at processImmediate [as _immediateCallback] (timers.js:336:15)
\
npm ERR! node_notes@0.0.1 start: `node ./bin/www`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the node_notes@0.0.1 start script.
npm ERR! This is most likely a problem with the node_notes package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get their info via:
npm ERR!     npm owner ls node_notes
npm ERR! There is likely additional logging output above.
npm ERR! System Windows_NT 6.1.7600
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! cwd E:\Do\hello_express\node_notes
npm ERR! node -v v0.10.29
npm ERR! npm -v 1.4.14
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     E:\Do\hello_express\node_notes\npm-debug.log
npm ERR! not ok code 0

這是我的異步代碼:

  function getActiveNotes(doc)
{
    var noteIds=doc.notes;
    async.map(noteIds,function(_id){
        notesCollection.findOne({_id:_id})
    },function(err,docs){
        if(err)
            console.error(err);
        else
        {
            if(docs)
                locals.notebook.notes=docs;
        }   
    });
}

function getAllNotebooks()
{
    notebookCollection.find({},{},function(err,docs){
        if(err)
            console.error(err);
        else
        {
            if(docs)
            {
                for(var i=0;i<docs.length;i++)
                {
                    locals.sidebar.items.push(docs[i].title);   
                }   
            }   
        }   
    });
}

function activeNotesOp(){
    async.waterfall([notebookCollection.findOne({isActive:true}),getActiveNotes]);  
}
async.parallel([getAllNotebooks,activeNotesOp],function(err,results){
    if(err)
        console.error(err);
    else
        console.log(results);
});

關於反射parallel可能不是您所需要的,但是我認為這對您是有用的。

var notebooksIdx = 0;

notebookCollection.find({},{},function(err,notebooks){

    async.each(
        notebooks,
    ,   function(notebook, cb) {

            notesCollection.find({_id: {$in: notebook.notes}}, function(err, notes) {

                if (err) 
                    return cb(err);

                return locals.notebook[notebooksIdx++].notes = notes;
            })
        }
    ,   function(err) {
            if (err) throw err;

            res.render(....)
        }
    });

});

我使用lodash方法forEach獲得每個notebook的標題以填充locals.sidebar ,我使用lodash.filter (這將返回一個數組,獲得了第一項)查找activeNotebook並使用$in從中檢索所有筆記運算符,以查詢筆記本中包含的所有筆記以及使用該數據填充的locals.notebook.notes

 notebookCollection.find({},{},function(err,notebooks){
    if(err)
        console.error(err);
    else
    {
        _(notebooks).forEach(function(notebook){
            locals.sidebar.items.push(notebook.title);
        });
        //console.log(locals.sidebar.items);
        var activeNotebooks= _.filter(notebooks,function(notebook){
            return notebook.isActive;
        });
        var notes=activeNotebooks[0].notes;
        notesCollection.find({_id:{$in:notes}},function(err,docs){
            if(err)
                console.error(err);
            else
            {
                if(docs.length)
                {
                    locals.notebook.notes=docs;
                    console.dir(locals);
                    res.render('main',locals);
                }   
            }   
        });
    }       
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM