【问题标题】:Having ejs error SyntaxError: missing ) after argument list in C:\Users\computer point\Desktop\project2\views\home.ejs while compiling ejs在编译 ejs 时在 C:\Users\computer point\Desktop\project2\views\home.ejs 中的参数列表后出现 ejs 错误 SyntaxError: missing )
【发布时间】: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() 并传递数据的代码,以便我们可以准确地看到您传递给模板的内容?还有,&lt;%}%&gt; 应该是什么?
  • 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


【解决方案1】:

为了在模板中使用数据,您必须将数据传递给res.render()res.render() 的格式是这样的:

res.render(view [, locals] [, callback])

第二个参数locals 是您要传递给模板的数据。我无法从您的问题中准确判断您要做什么或哪个模板是哪个模板,但看起来您可以这样做:

app.post('/compose',function(req,res){ 
    let posts = [{ title : req.body.title, post : req.body.post }];
    res.render("compose", posts);
});

并且,将您的撰写模板更改为:

<%- include('header'); -%>
<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>
<ul>
<% posts.forEach(function(post) { %>
    <li><%= post.title %></li>
<%})%>
</ul>

<%- include('footer'); -%>

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2019-05-27
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多