gpt4 book ai didi

node.js - 比较 Handlebars 的 block 助手

转载 作者:太空宇宙 更新时间:2023-11-04 00:19:40 29 4
gpt4 key购买 nike

我在代码中使用 block 助手比较值,但每次运行此代码时出现问题,我都会在网页 View 上收到此错误消息

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"> &laquo;</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');

哪里错了?

最佳答案

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');

关于node.js - 比较 Handlebars 的 block 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888934/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com