Render multiple query result

Render multiple query result



In my HTML page, I need to show the total number of items bought this day and below I have to display all the datas. Its a simple thing but I am not sure how to do it in NodeJS.



I am trying following code:


var HL = require('../modules/history-list');

app.get('/activity', function (req, res)
if (req.session.user == null)
res.redirect('/');
else
HL.getTotalRecords(function (e, count)
res.render('activity',
title: 'My Activity',
udata: req.session.user,
count: count
);
);

HL.getAllRecords(function (e, activities)
res.render('activity',
title: 'My Activity',
udata: req.session.user,
activities: activities
);
);

);


exports.getTotalRecords = function (callback)
let start = moment().startOf('day');
let end = moment().endOf('day');

history.aggregate([
"$addFields":
"date":
"$dateFromString":
"dateString": "$date"



,

"$match":
"date":
"$gte": start.toDate(),
"$lt": end.toDate()


,

"$count": "count"

]).toArray(function (e, res)
if (e) callback(e)
else callback(null, res);
);


exports.getAllRecords = function (callback)
history.find(, function (e, res)
if (e) callback(e)
else callback(null, res);
);




I am getting Error: Can't set headers after they are sent. error as I have res.render twice. I am not sure how can I fix it.


Error: Can't set headers after they are sent.



Any help will be appreciated.



Thank You





It happens because you call another res.render after already rendering your totalRecords. You might want your getTotalRecords and getAllRecords to return proper records, store them in some collection, then render both at one time.
– dziraf
Aug 24 at 5:32



res.render


totalRecords


getTotalRecords


getAllRecords




1 Answer
1



Try this,


var HL = require('../modules/history-list');

app.get('/activity', function (req, res)
if (req.session.user == null)
res.redirect('/');
else
HL.getTotalRecords(function (e, count)
HL.getAllRecords(function (e, activities)
res.render('activity',
title: 'My Activity',
udata: req.session.user,
count: count,
activities: activities
);
);
);

);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)