为什么我得到缺少帮助程序错误:部分包含以前有效的代码



我正在尝试使用 express 处理对表单的 get 请求,我的代码如下:

app.get('/newsletter', function(req, res){
res.render('newsletter');
});

发出请求时出现此错误:

> Error: Missing helper: 'section'
>     at new Exception (G:expressnode_moduleshandlebarsdistcjshandlebarsexception.js:13:41)
>     at Object.<anonymous> (G:expressnode_moduleshandlebarsdistcjshandlebarsbase.js:57:13)
>     at Object.eval (eval at createFunctionContext (G:expressnode_moduleshandlebarsdistcjshandlebarscompilerjavascript-compiler.js:189:23),
> <anonymous>:18:213)
>     at G:expressnode_moduleshandlebarsdistcjshandlebarsruntime.js:86:31
>     at G:expressnode_moduleshandlebarsdistcjshandlebarscompilercompiler.js:465:21
>     at ExpressHandlebars._renderTemplate (G:expressnode_modulesexpress3-handlebarslibexpress-handlebars.js:339:22)
>     at ExpressHandlebars.renderTemplate (G:expressnode_modulesexpress3-handlebarslibexpress-handlebars.js:196:18)
>     at fn (G:expressnode_modulesasynclibasync.js:582:34)
>     at Immediate.<anonymous> (G:expressnode_modulesasynclibasync.js:498:34)
>     at runCallback (timers.js:789:20)

想通了......我需要将助手对象添加到我的车把配置中......以下是更正我的代码的补充;

const handlebars = require('express3-handlebars').create(
{defaultLayout:'main',
helpers:{
section:function(name, options){
if(!this._sections){this._sections = {}};
this._sections[name] = options.fn(this);
return null;
};
}});

最新更新