Node.js NodeJS、Express、PUG-需要将mongodb中的结果呈现给PUG视图中包含的变量
我有一个网站,用于保存文件和通过表格标记条目。我目前正在尝试查找mongo集合中的所有文档,然后使用Node.js NodeJS、Express、PUG-需要将mongodb中的结果呈现给PUG视图中包含的变量,node.js,mongodb,express,Node.js,Mongodb,Express,我有一个网站,用于保存文件和通过表格标记条目。我目前正在尝试查找mongo集合中的所有文档,然后使用res.render()方法填充documents.pug中的变量 我尝试将res.render放入for循环中,然后将var I传递到我的results参数中。经过一些阅读,我发现我不能多次执行res.render(for loop.) 我有一个模型设置使用猫鼬。我能够找到集合中的所有文档并将其返回。这里我的问题是用Documents.find({})(mongoose)返回的结果填充通过res
res.render()
方法填充documents.pug
中的变量
我尝试将res.render
放入for循环中,然后将var I
传递到我的results参数中。经过一些阅读,我发现我不能多次执行res.render
(for loop.)
我有一个模型设置使用猫鼬。我能够找到集合中的所有文档并将其返回。这里我的问题是用Documents.find({})
(mongoose)返回的结果填充通过res.render()
定义的pug变量
也许有人能帮我找出解决这个问题的办法吗
document.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);
}
哈巴狗
扩展布局
区块干线
div(class=“容器”)
表(class=“uk表uk表条纹uk表小”)
泰德
tr
第四个文件ID
标题
第th类
第三作者
描述
设计表尾样式
表格主体
-对于(变量i=0;i<5;++i)
tr
td=文档id
td=文件标题
td=文件类别
td=文档作者
td=文件描述
您正在使用未定义的值呈现表单,因为文档是一个数组,而数组没有文档属性
将res.render
更改为
res.render('documents', {
pageTitle: "Documents",
docs: docs
});
和帕格观点:
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
- each doc in docs
tr
td= doc.doc_id
td= doc.doc_title
td= doc.doc_category
td= doc.doc_author
td= doc.doc_description
哎呀。。。。这就更有意义了我想是时候退一步,多想想了。非常感谢@hya!这很有魅力。
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
- each doc in docs
tr
td= doc.doc_id
td= doc.doc_title
td= doc.doc_category
td= doc.doc_author
td= doc.doc_description