Sending javascript variable on res.render - express
10,558
You are declaring your variables in the wrong scope. You are also overwriting them on each of the forEach loop executions
app.get('/:verb', function(req, res) {
var verbName,
description;
if(!!req.param.verb) {
config.data.forEach(function(o) {
verbName = o.name,
description = o.description;
});
};
res.render('verb', {title: verbName, subtitle: description});
});
Author by
Peachey_A
Updated on June 04, 2022Comments
-
Peachey_A almost 2 years
I'm trying to build a small app to call from a
config.js
file containing JSON and write that data to a page based on thename
key within the JSON.app.get('/:verb', function(req, res) { if(!!req.param.verb) { config.data.forEach(function(o) { var verbName = o.name, description = o.description; }); }; res.render('verb', {title: verbName, subtitle: description}); });
What I'm trying to do is use the
verbName
anddescription
javascript variables as Jade variable in theres.render
structure. As it stands this code will fail due toverbName
anddescription
not being strings.Is it possible to include variables this way?
PS - been in express 1 week and Jade 2 days, so all ideas/solutions would be appreciated.