我正在使用 Express、EJS 和 mongoose 进行网站学习练习。
这是我的 index.js 模块:
blog_model = require('./connection').blog_model;
/*
* GET home page.
*/
exports.index = function(req, res)
{
var rubric, url, logout, tunes;
if(req.user != null)
{
url='/internal_index';
rubric='To the backroom, ' + req.user.fullName;
logout='logout';
}
else
{
url='/login';
rubric='login';
logout='';
}
blog_model.find(({}, function(err, tunes)
{
console.log('tunes: ' + tunes)
res.render('index', { title: 'blah',
url : url,
rubric : rubric,
logout : logout
});
}))
};
我收到以下错误:
SyntaxError: Unexpected token for
at Object.Function (<anonymous>)
at exports.compile (/Users/chrisaugier/Web/frontRoom/node_modules/ejs/lib/ejs.js:234:12)
at Object.exports.render (/Users/chrisaugier/Web/frontRoom/node_modules/ejs/lib/ejs.js:273:10)
at View.exports.renderFile [as engine] (/Users/chrisaugier/Web/frontRoom/node_modules/ejs/lib/ejs.js:303:22)
at View.render (/Users/chrisaugier/Web/frontRoom/node_modules/express/lib/view.js:75:8)
at Function.app.render (/Users/chrisaugier/Web/frontRoom/node_modules/express/lib/application.js:506:10)
at ServerResponse.res.render (/Users/chrisaugier/Web/frontRoom/node_modules/express/lib/response.js:756:7)
at Promise.<anonymous> (/Users/chrisaugier/Web/frontRoom/routes/index.js:28:9)
at Promise.<anonymous> (/Users/chrisaugier/Web/frontRoom/node_modules/mongoose/node_modules/mpromise/lib/promise.js:162:8)
但是对数据库的查询正在工作。
非常感谢任何帮助。
克里斯
编辑
以下是我的 EJS 模板代码。潜在的问题部分是最近添加的 for 循环。但是我尝试的第一件事是将其注释掉并且仍然给出相同的错误。
<!DOCTYPE html>
<html>
<head>
<title>title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel='stylesheet' type="text/css" href="stylesheets/grid.css"/>
<link rel='stylesheet' type="text/css" href="stylesheets/normalize.css"/>
<link rel='stylesheet' type="text/css" href="stylesheets/style.css"/>
<body>
<div class="loginbar">
<a href="<%= url%>"><%= rubric%></a>
<a href='/logout'><%= logout%></a>
</div>
<div class="slide" id="slide1" data-slide="1" data-stellar-background-ratio="0.5">
<div class="container clearfix">
<div id="header" class="grid_7">
<h1>frontRoom</h1>
<h2>...don't go in the back room.</h2>
<p><i>"Let " </i> - Bagehot</p>
</div>
<ul class="navigation">
<li data-slide="1">Tunes</li>
<li data-slide="2">Collaborators</li>
<li data-slide="3">Blog</li>
<li data-slide="4">About</li>
</ul>
</div>
</div>
<div class="slide" id="slide2" data-slide="2" data-stellar-background-ratio="0.5">
<div class="container clearfix">
<h1>Tunes</h1>
<h2>Latest reviews</h2>
<%= for (vari=0;i<tunes.length;i++){%>
<div class="grid_5">
<h3><%= tunes[i].title %></h3>
<p><%= tunes[i].body %></p>
</div>
<%= }%>
</div>
</div>
<div class="slide" id="slide3" data-slide="3" data-stellar-background-ratio="0.5">
<div class="container clearfix">
<h1>Collaborators</h1>
<div class="grid_3">
<h2>blah</h2>
<p>Lore.</p>
</div>
<div class="grid_3">
<h2>blah/h2>
<p>Loremnim. </p>
</div>
<div class="grid_3">
<h2>Cblah</h2>
<p>Sed pretiagna eu.</p>
</div>
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.stellar.min.js"></script>
<script type="text/javascript" src="js/waypoints.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>
</body>
</html>