【问题标题】:Missing helper "select"?缺少助手“选择”?
【发布时间】:2019-04-06 08:17:53
【问题描述】:

我正在尝试从 mongodb 中显示选定的选项。我曾多次尝试通过检查不同的来源来解决这个问题。我收到此错误(错误:缺少助手:“选择”)

这是我的 handlebar_helper.js 文件

module.exports = (Handlebars)=>{
  Handlebars.registerHelper('select', function(selected, options) {
    return options.fn(this).replace(new RegExp('value = \"'+ selected + '\"'), '$&selected="selected"');
  });

};

这是我的主文件 app.js

const {select} = require('./helpers/handlebars_helpers');

app.engine('handlebars', exphbs({defaultLayout: 'home', helpers: {select: select}}));
app.set('view engine', 'handlebars');

这里是edit.handlebars

{#select post.status}}
            <option value="public">Public</option>
            <option value="private">Private</option>
            <option value="draft">Draft</option>
    {{/select}}

【问题讨论】:

    标签: node.js regex express


    【解决方案1】:

    如果您要将辅助函数作为参数传递,则无需调用registreHelper。只需导出函数本身”:

    handlebar_helper.js:

    module.exports = function(selected, options) {
        return options.fn(this).replace(new RegExp('value = \"'+ selected + '\"'),'$&selected="selected"');
    }
    

    其他一切都应该保持不变。

    当然,这使得帮助文件的名称有点奇怪。我会将其重命名为 select_helper.js - 但这只是一种意见。

    【讨论】:

    • 我是否必须将某些内容更改为选项标签内的 edit.handlebars 文件
    • 我更改了名称 helpers:{selectOption : select} 以及 edit.handlebars 和 handlebar_helper.js 现在它显示 selectOption 缺失
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多