node.js - How to render the frontpage with two or more queries -
we have express server configured in file called app.js
, our crowdfunding platform.
each page declared app.get
& res.render
, this:
app.get('/some-page', function (req, res) { res.render('some-page', { title: 'some-title' }) })
on our frontpage, include query amount on 1 of crowdfunding campaigns, show current status. this:
app.get('/frontpage', function (req, res) { payment.filter({ projectname: 'testcampaign' }) .sum("amount") .execute().then(function (amountpledged) { res.render('frontpage', { amountpledged: amountpledged / 100, }) }) })
problem is, structure makes hard have 2 (or more) queries current campaigns.
we need way have 2 (or more) different amountpledged
on our frontpage @ same time (maybe amountpledged
, amountpledgedsecond
).
if want have two/more queries, can define two/more queries(which return promises) , use promise.all
outputs. if problem think is.
you can below:
app.get('/frontpage', function (req, res) { var p1 = payment.filter({ projectname: 'test1campaign' }) .sum("amount") .execute() var p2 = payment.filter({ projectname: 'test1campaign' }) .sum("amount") .execute() promise.all([p1, p2]).then(values => { // values array of resolved value of respective promises // can manipulate array desired value(like diving element 100) res.render('frontpage', { amountspledged: values, }) }); })
Comments
Post a Comment