I'm having a problem using handlebars in my project, I'm developing an app with Node Express for college and I would like some help.
I can't make Handlebars display values in a specific case, when i send one object dataset from an MySQL query(I'm using the mysql module for Node), then I can't use any other Handlebars expressions, like {{#if}} or another {{#each}} loops inside this first #each loop, in my code below, the "#each ngos", it works fine and fills the small tags with the right values, but the #if conditional does not work, inside this outer #each ngos, it does not display the values for inner loops or inner conditionals. This is my html handlebars file:
{{#each ngos}}
<div class="card mt-4">
<div class="card-body">
<h4>{{name_ngo}}</h4>
<small>Department: {{department}}</small><br>
<small>Need: {{need}}</small><br>
{{#if login}}
<a href="/ong/{{id_ngo}}"><button class="btn btn-primary mt-4">Edit NGO</button></a>
<a href="/delong/{{id_ngo}}"><btton class="btn btn-danger mt-4">Delete NGO</button></a>
{{/if}}
</div>
</Div>
{{else}}
<div class="alert alert-danger">{{error_msg}}</div>
{{/each}}
This is how I'm sending data to res.render(), I've tried to send the req.session.login in an atempt to validate if the user can acess certain buttons.
res.render('ngos',{ngos,login : req.session.login})
This is an example dataset:
[
{
id_ngo: 21,
name_ngo: 'Some name',
department: 'food',
need: 'food'
}
]
This is my Handlebars setup:
app.use(BodyParser.urlencoded({extended: true}));
app.use(BodyParser.json());
app.use((req,res,next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
next();
})
app.engine('handlebars', engine());
app.set('view engine','handlebars');
app.set("views",'./views');
I've read just about every question here regarding Handlebars, but I can't understand how it works, I'm completely new to Handlebars and Node in general.
This is my first question here,so thanks in advance and please forgive me for my bad english.
Tested outside the outer #each ngos loop and it works fine.