使用玉石、快递和僧侣的所有数据渲染视图
Rendering view with all the data using jade,express and monk
我的mongodb
数据库有两个集合notes
和notebook
,前者保存每个音符,后者有一个标题和一个包含指向notes
集合中note
的id
的array
的notes
数组。
//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);
}
}
});
}
});
相关文章:
- 如何处理数据视图中项目的鼠标点击
- 具有多个控件的数据视图
- 在slickgrid数据视图中使用深度映射
- 数据视图和原型继承
- 对于光滑网格数据视图,如果有更多的行数,则不会加载所有行
- 创建一个滚动条一个ext js数据视图
- 如何使用外部存储呈现数据视图的 tpl
- Google 图表数据视图动态设置列
- Slickgrid-如何更新数据视图中的数据
- 如何动态重新加载任何slickgrid数据视图
- extjs数据视图未找到事件选择
- 如何将数据添加到SlickGrid数据视图
- 如何使用函数对Google Charts数据表或数据视图进行排序
- Sencha Touch:数据视图上的组件,看起来像列表组头
- 数据视图滚动问题sencha触摸
- EXTJS 3.4如何在选择更改时获取记录数据视图
- 数据视图样本在MVC架构下不工作
- Extjs数据视图不显示图像
- Extjs 4数据视图拖放(复制)只工作一次
- 谷歌图表API无法动态设置数据视图列