【发布时间】:2019-08-20 20:12:38
【问题描述】:
我正在尝试通过 ejs 从 app.js 获取数据,但它给出了错误。
计算机科学学生
<%- include('header'); -%>
<h1><%= foo%></h1>
<p class = "home-content">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<% for(var i = 0 ;i< posts.length;i++)%>
<%console.log(posts[i].title);%>// THIS LINE GIVES AN ERROR AND I AM UNANBLE TO POST THIS ON HOME.EJS
<%}%>
<%- include('footer'); -%>
我相信有人帮助我消除此错误
【问题讨论】:
-
为什么要在模板的渲染中插入
console.log()?这似乎不合适。另外,请显示准备您传递给模板的数据的代码,并显示调用res.render()并传递数据的代码,以便我们可以准确地看到您传递给模板的内容?还有,<%}%>应该是什么? -
app.post('/compose',function(req,res){ newData = req.body.title; var post = [{ match : req.body.title, post : req.body .post }] posts.push(post); res.redirect("/"); }); app.get('/compose',function(req,res){ res.render('compose'); })
-
如果我们删除 console.log 也会报错
-
cmets 中的多行代码不可读。请使用编辑链接将代码添加到您的问题中。
标签: javascript express ejs