缺少助手“选择"? [英] Missing helper "select"?

查看:32
本文介绍了缺少助手“选择"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示mongodb中的选定选项.我已经尝试过很多次,通过检查不同的来源来解决此问题.我收到此错误(错误:缺少帮助程序:选择")

I m trying to show the selected option from the mongodb. I've tried to many times to solve this problem from checking different sources. i got this error ( Error: Missing helper: "select")

这是我的handlebar_helper.js文件

here is my handlebar_helper.js file

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

};

这是我的主文件app.js

here is my main file app.js

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

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

这是edit.handlebars

here is edit.handlebars

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

推荐答案

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

You don't need to call registreHelper if you are going to pass helper functions as arguments. Just export the function itself":

handlebar_helper.js:

handlebar_helper.js:

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

其他所有内容都应保持不变.

everything else should work unchanged.

当然,这会使助手文件的名称有些奇怪.我将其重命名为 select_helper.js -但这只是一个意见.

Of course, this makes the name of the helper file a bit weird. I'd rename it to select_helper.js - but that's just an opinion.

这篇关于缺少助手“选择"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆