Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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")

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

};

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

here is edit.handlebars

{#select post.status}}
            <option value="public">Public</option>
            <option value="private">Private</option>
            <option value="draft">Draft</option>
    {{/select}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
270 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

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

everything else should work unchanged.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...