【发布时间】:2017-08-01 03:42:25
【问题描述】:
我有一个网站,用于保存文件和通过表格标记条目。我目前正在尝试查找 mongo 集合中的所有文档,然后使用 res.render() 方法填充 documents.pug 中的变量。
我尝试将res.render 放在for 循环中,然后将var i 传递给我的结果参数。经过一番阅读,我发现我不能多次执行res.render(for循环。)。
我有一个使用猫鼬的模型设置。我能够找到集合中的所有文档并返回它们。我的问题是用Documents.find({})(mongoose) 返回的结果填充我通过res.render() 定义的pug 变量。
谁能帮我找出解决办法?
文档.js
module.exports = (app) => {
// Creating new instance of router for docsPage.
const docsRouter = require('express').Router();
// Creating Document const which will contain the documents model schema.
const Document = require('../models/document.model');
docsRouter.route('/')
.get((req, res) => {
Document.find((err, docs) => {
if (err) {
console.error(err);
res.send(err);
}
else {
// console.log(docs[0].doc_id);
res.render('documents', {
pageTitle: "Documents",
doc_id: docs.doc_id,
doc_title: docs.doc_title,
doc_category: docs.doc_category,
doc_author: docs.doc_author,
doc_description: docs.doc_description
});
}
});
})
app.use('/documents', docsRouter);
}
文件.pug
extends layout
block main
div(class="container")
table(class="uk-table uk-table-striped uk-table-small")
thead
tr
th Document ID
th Title
th Category
th Author
th Description
tfoot
tbody
- for (var i = 0; i < 5; ++i)
tr
td= doc_id
td= doc_title
td= doc_category
td= doc_author
td= doc_description
【问题讨论】: