在我的HTML页面中,我需要显示当天购买的商品总数,并且在下面必须显示所有数据。这很简单,但是我不确定如何在NodeJS中做到这一点。
我正在尝试以下代码:
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);
});
}
我两次重新渲染都遇到Error: Can't set headers after they are sent.
错误。我不确定该如何解决。
任何帮助将不胜感激。
谢谢
答案 0 :(得分:1)
尝试一下
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
});
});
});
}
});