【发布时间】:2014-10-02 12:20:58
【问题描述】:
我的mongodb 数据库有两个集合notes 保存每个注释,notebook 有一个标题和一个notes 数组,其中包含一个array 的ids 指向note 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")
]
}
我使用这些来检索数据to render this jade view。这是我的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),以便它在此处呈现包含我所有数据的模板?
使用异步时编辑错误:
使用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);
});
【问题讨论】:
-
我建议您使用 2 个嵌套的 async.parallel 命令(一个用于获取 Notebooks;一个用于获取 Notes)并在外部循环完成后运行您的渲染函数github.com/caolan/async#parallel
-
@SimonH 我添加了
async库,我在这里要做的是拥有多个笔记本,将其中一个笔记本标记为活动并检索该笔记本的数据。我使用waterfall检索活动笔记本及其笔记和平行检索所有笔记本标题。但我收到此错误,我已添加到问题中
标签: javascript node.js mongodb express pug