express-Handlebars: Error: Missing helper: "for"
express-Handlebars: Error: Missing helper: "for"
我正在尝试一个非常基本的车把模板,如下所示:
// movies.hbs
<h1>Movies</h1>
<ul>
{{#for movies}}
{{#if this}}
<li><a href="http://www.imdb.com/title/{{ this.title }}">{{ this.title }}, {{ this.year }}
{{this.genre}}</a>
</li>
{{else}}
<li>No movies found.</li>
{{/if}}
{{/for}}
</ul>
然而,当我使用从 MongoDB 数据库中检索到的文档渲染它时,
MongoClient.connect("mongodb://localhost:27017/vidoes", function(err, db) {
if (err) throw err;
db.collection('movies').find({}).toArray(function(err, docs) {
if (err) throw err;
app.get('/', function(req, res){
res.render('movies', {'movies':docs});
});
app.use(function(req, res){
res.sendStatus(404);
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Express server listening on port %s.", port);
});
});
});
我收到奇怪的错误:
$ node app.js
Express server listening on port 3000.
Error: Missing helper: "for"
at Object.<anonymous> (/public_html/All Together Now/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
at Object.eval [as main] (eval at createFunctionContext (/public_html/All Together Now/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:6:89)
当我在没有数据库的情况下尝试时,它给了我同样的错误。当然是在我访问首页之后。
app.get('/', function(req, res){
var docs = [{ "title" : "Minuscule: Valley of the Lost Ants (Minuscule - La vallée des fourmis perdues)", "genre" : "Adventure|Animation|Children", "year" : 1993 },
{ "title" : "Bambi Meets Godzilla", "genre" : "Animation|Comedy", "year" : 2003 },
{ "title" : "Annie Get Your Gun", "genre" : "Musical|Western", "year" : 2011 },
{ "title" : "Scary Movie 5 (Scary MoVie)", "genre" : "Comedy", "year" : 2005 }
];
res.render('movies', {'movies':docs});
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Express server listening on port %s.", port);
});
如有任何帮助,我们将不胜感激。
CS
你应该试试 each
。往下看
// file tweets.js model
let TweetSchema = mongoose.Schema({
text: String,
userId: String,
createdAt: Date
});
let Tweet = mongoose.model('tweets', TweetSchema);
module.exports = Tweet;
// on server side index.js
routerPublic.get('/', (request, response) => {
Tweet.find({}).sort('-createdAt').exec(function (error, documents) {
response.render('pages/home', { user: request.user, tweets: documents });
});
});
// in home.jbs
<h2>Tweets</h2>
{{# each tweets }}
<div class="panel panel-default">
<div class="panel-body">
{{ text }}
</div>
<div class="panel-footer">
{{ userId }} • {{ niceTime createdAt }}
</div>
</div>
{{ else }}
<div class="panel panel-default">
<div class="panel-body">
No tweets yet. <a href="/tweet">Click here</a> to Tweet.
</div>
</div>
{{/each}}
如果你想要一些额外的东西,那么我会参考创建助手
https://gist.github.com/LeCoupa/6176077a9a8e2ad00eda
我正在尝试一个非常基本的车把模板,如下所示:
// movies.hbs
<h1>Movies</h1>
<ul>
{{#for movies}}
{{#if this}}
<li><a href="http://www.imdb.com/title/{{ this.title }}">{{ this.title }}, {{ this.year }}
{{this.genre}}</a>
</li>
{{else}}
<li>No movies found.</li>
{{/if}}
{{/for}}
</ul>
然而,当我使用从 MongoDB 数据库中检索到的文档渲染它时,
MongoClient.connect("mongodb://localhost:27017/vidoes", function(err, db) {
if (err) throw err;
db.collection('movies').find({}).toArray(function(err, docs) {
if (err) throw err;
app.get('/', function(req, res){
res.render('movies', {'movies':docs});
});
app.use(function(req, res){
res.sendStatus(404);
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Express server listening on port %s.", port);
});
});
});
我收到奇怪的错误:
$ node app.js
Express server listening on port 3000.
Error: Missing helper: "for"
at Object.<anonymous> (/public_html/All Together Now/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13)
at Object.eval [as main] (eval at createFunctionContext (/public_html/All Together Now/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:6:89)
当我在没有数据库的情况下尝试时,它给了我同样的错误。当然是在我访问首页之后。
app.get('/', function(req, res){
var docs = [{ "title" : "Minuscule: Valley of the Lost Ants (Minuscule - La vallée des fourmis perdues)", "genre" : "Adventure|Animation|Children", "year" : 1993 },
{ "title" : "Bambi Meets Godzilla", "genre" : "Animation|Comedy", "year" : 2003 },
{ "title" : "Annie Get Your Gun", "genre" : "Musical|Western", "year" : 2011 },
{ "title" : "Scary Movie 5 (Scary MoVie)", "genre" : "Comedy", "year" : 2005 }
];
res.render('movies', {'movies':docs});
});
var server = app.listen(3000, function() {
var port = server.address().port;
console.log("Express server listening on port %s.", port);
});
如有任何帮助,我们将不胜感激。
CS
你应该试试 each
。往下看
// file tweets.js model
let TweetSchema = mongoose.Schema({
text: String,
userId: String,
createdAt: Date
});
let Tweet = mongoose.model('tweets', TweetSchema);
module.exports = Tweet;
// on server side index.js
routerPublic.get('/', (request, response) => {
Tweet.find({}).sort('-createdAt').exec(function (error, documents) {
response.render('pages/home', { user: request.user, tweets: documents });
});
});
// in home.jbs
<h2>Tweets</h2>
{{# each tweets }}
<div class="panel panel-default">
<div class="panel-body">
{{ text }}
</div>
<div class="panel-footer">
{{ userId }} • {{ niceTime createdAt }}
</div>
</div>
{{ else }}
<div class="panel panel-default">
<div class="panel-body">
No tweets yet. <a href="/tweet">Click here</a> to Tweet.
</div>
</div>
{{/each}}
如果你想要一些额外的东西,那么我会参考创建助手 https://gist.github.com/LeCoupa/6176077a9a8e2ad00eda