我正在学习 Express 和 Pug,但无法在多个页面上显示内容。
我正在使用表单创建一篇文章,将标题+内容保存到数据库中。使用此代码,我将文章呈现到“查看所有文章”页面。
// Display list of all articles.
exports.article_list = function (req, res, next) {
Article.find({}, "title author")
.sort({ title: 1 })
.populate("author")
.exec(function (err, list_articles) {
if (err) {
return next(err);
}
//Successful, so render
res.render("article_list", {
title: "Article List",
article_list: list_articles,
});
});
};
在我的
article_list
pug
文件中,我有这段代码可以显示所有文章的列表。
extends layout
block content
h1= title
ul
each article in article_list
li
a(href=article.url) #{article.title}
| by Adam
else
li There are no articles.
但是,当我将相同的代码添加到我的
index
pug
文件中时,出现以下错误:
Cannot read properties of undefined (reading 'length')
我不仅希望文章显示在“查看所有文章”部分,而且我希望最近提交的前 5 或 10 篇文章显示在主页上,但这样做有问题。