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});
    });
Share:
10,558
Peachey_A
Author by

Peachey_A

Updated on June 04, 2022

Comments

  • Peachey_A
    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 the name 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 and description javascript variables as Jade variable in the res.render structure. As it stands this code will fail due to verbName and description 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.