node.js - 服务器打开 SyntaxError: Unexpected token = in /home/michael/DemoApp/views/posts.ejs while compile ejs
问题描述
服务器已打开
SyntaxError: Unexpected token = in /home/michael/DemoApp/views/posts.ejs 同时编译 ejs
const express = require("express");
const app = express();
const port = process.env.PORT || 4000
app.set('views', './views');
app.set('view engine', 'ejs');
app.get("/posts", function(req, res){
var posts = [
{title: "Once upon a time", author: "Richard"},
{title: "The Way", author: "Ikoro"},
{title: "I Love Critics", author: "Emmanuel"},
{title: "Adorable Bunny", author: "Christian"}
]
res.render("posts.ejs", {posts: posts});
});
app.listen(port, function(){
console.log("Server is open");
});
// The Codes for posts.ejs
<% for(var = i; i < posts.length; i++){ %>
<li>
<%= posts[i].title %> - <strong><%= posts[i].author
%></strong>
</li>
<% } %>
服务器已打开
SyntaxError: Unexpected token = in /home/michael/DemoApp/views/posts.ejs 同时编译 ejs
解决方案
在您的代码中var = i
- 这是错误。它应该是:
<% for(var i = 0; i < posts.length; i++){ %>
<li>
<%= posts[i].title %> - <strong><%= posts[i].author
%></strong>
</li>
<% } %>
推荐阅读
- java - Firebase 数据库查询未显示在屏幕上
- javascript - 如何使用 Event Emitter 接收多个相同类型的事件?
- java - J2SE和J2EE中的2代表什么?
- python - 'array[:] = something' 和 'array = something' 之间的区别
- python - 具有已定义名称范围的 Pandas 数据框到 Excel
- accessibility - 辅助功能:相关内容 - 两指轻弹
- sql - 分配一个唯一的 id sql server
- sql - 如何根据行号动态创建新列
- vue.js - 无法从 rootState 访问 Vuex 模块
- r - 如何使用windows scheduler执行R脚本