gpt4 book ai didi

node.js - jade循环无法访问node.js文件中的变量

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:29 25 4
gpt4 key购买 nike

我几天来一直在寻找这个问题的解决方案,并且希望得到任何解决方案或建议。

我正在根据this how to node tutorial使用node.js、express和mongo db构建一个非常简单的博客。我这样做是为了更好地理解 Node ,因为我还不是很熟悉。

我遵循了所有说明,考虑到版本差异,一切都运行良好,直到我到达教程的标题“A View to Kill”,其中我将一些虚拟数据发送到要渲染的 Jade 文件。

如果我将代码保留在 app.js 文件中,并且没有向其中传递 jade 文件:

var articleProvider= new ArticleProvider();

app.get('/', function(req, res){
articleProvider.findAll(function(error, docs){
res.send(docs);
});
})

app.listen(3000);

我得到了数据对象的渲染。

但是当我传入要渲染的jade文件时,我收到错误:

TypeError: Cannot read property 'length' of undefined

...它指的是我的 jade 文件中的每个循环(第 3 行),位于此处:

h1= title
#articles
- each article in articles
div.article
div.created_at= article.created_at
div.title
a(href="/blog/"+article._id)!= article.title
div.body= article.body

Here is the link到这整个该死的事情的根源。非常感谢任何帮助!

最佳答案

articles 引用未定义。尝试将渲染代码更改为以下内容:

app.get('/', function(req, res){
articleProvider.findAll(function(error, docs){
res.render('index.jade', {
articles: docs
});
});
});

关于node.js - jade循环无法访问node.js文件中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25069410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com