Sunday 2 April 2017

iterative render with node and express

New to node and async and still struggling with concepts.

Trying to use express/handlebars render with a callback to iteratively build an html body with content from an array. End goal is to send a response with a number of emails each one individually rendered using a view.hbs.

Got this far but realised it was never going to work. res.render can't pass my html variable back in the callback and res.send would run before the renders have completed???

function buildRes (req, res, email) {
    var html = '';
    Object.keys(email).forEach(function (i) { 
        res.render('emailPanel', {subject: email[i].subject, body: email[i].body},
            function(err, renOut) {
                if err throw err;
                html=html+renOut;
            }
        )
    })
    res.send(html);
}

Any suggestions on how I should be approaching this problem?

Started out trying to use handlebars #each helper to do the iteration but all of the examples show a simple list whereas in my case there a multiple array parameters to be passed to the render.



via PJ62

No comments:

Post a Comment