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
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.
It happens because you call another
res.render
after already rendering yourtotalRecords
. You might want yourgetTotalRecords
andgetAllRecords
to return proper records, store them in some collection, then render both at one time.– dziraf
Aug 24 at 5:32