我在代码中使用块帮助程序来比较值,但是每次运行此代码时都会出现问题我在我的网页视图中得到了这个错误按摩
Error: Missing helper: "compare" at Object.<anonymous> (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13) at Object.eval (eval at createFunctionContext (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:8:91) at main (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/handlebars/dist/cjs/handlebars/runtime.js:175:32) at ret (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/handlebars/dist/cjs/handlebars/runtime.js:178:12) at ret (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21) at ExpressHandlebars._renderTemplate (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/express-handlebars/lib/express-handlebars.js:247:12) at ExpressHandlebars.<anonymous> (/opt/lampp/htdocs/x/chessMult/regtest/using-passport-with-sequelize-and-mysql-master/node_modules/express-handlebars/lib/express-handlebars.js:173:21)
我的index.hbs文件
{{#compare pageCount ">" 1 }}
<ul class="pagination"></ul>
{{#compare currentPage ">" 1 }}
<li><a href="/?page=1"> «</a></li>
{{/compare}}
{{/compare}}
我的auth.js文件
res.render('index', {x:"1888", "personList": personList,data: data, pageSize: pageSize, pageCount: pageCount,currentPage: currentPage});
我的app.js文件
const exphbs = require('express-handlebars');
const hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
foo: function () { return 'FOO!'; },
extname: ".hbs",
compare: function(lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
var operator = options.hash.operator || "==";
var operators = {
'==': function(l,r) { return l == r; },
'===': function(l,r) { return l === r; },
'!=': function(l,r) { return l != r; },
'<': function(l,r) { return l < r; },
'>': function(l,r) { return l > r; },
'<=': function(l,r) { return l <= r; },
'>=': function(l,r) { return l >= r; },
'typeof': function(l,r) { return typeof l == r; }
}
if (!operators[operator])
throw new Error("Handlerbars Helper 'compare' doesn't know the operator "+operator);
var result = operators[operator](lvalue,rvalue);
if( result ) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
}
});
//For Handlebars
app.set('views', './app/views')
app.engine('.hbs', exphbs({extname: '.hbs'}));
app.set('view engine', '.hbs');
哪里错了?
答案 0 :(得分:1)
app.js
档
const exphbs = require('express-handlebars');
const hbs = exphbs.create({
//...
});
//For Handlebars
app.set('views', './app/views');
app.engine('.hbs', hbs.engine); // <===== Use `hbs.engine`
app.set('view engine', '.hbs');