我的API中有一条路线。当您应用于此路由器时,将生成最近7天,并在一天中的每一天从数据库中提取元素。但显然是因为异步,样本不能正常运行。
apiRoutes.get('/costs/chart', function (req, res) {
var currentDay = moment().format('L');
var currentWeekDays = [];
for (var i = 0; i < 7; i++) {
var day = {
date: (function () {
var date = moment().add(-[i], 'd');
return moment(date).format('L');
})(),
costs: []
};
Cost.find({formatDate: day.date}, function (err, costs) {
if (costs.length > 0) {
console.log(costs);
day['costs'] = costs;
}
});
currentWeekDays.push(day);
}
var result = {
content: {
currentDay: currentDay,
currentWeekDays: currentWeekDays
}
};
res.json(result);
});