У меня есть два разных маршрутизатора, и оба дают точный результат в случае отдельных просмотров.Но я хочу получить оба результата в одном представлении (индекс).Вот первый маршрутизатор:
router.get('/', ensureAuthenticated, (req, res) => {
if(req.query.search){
const regex = new RegExp(escapeRegex(req.query.search), 'gi');
Idea.find({itemName: regex}, function(err, ideas){
if(err){
console.log(err);
}
else{
res.render('ideas/index', {
ideas:ideas
});
}
});
}
else {
Idea.find({})
.sort({date:'desc'})
.then(ideas => {
res.render('ideas/index',{
ideas:ideas
});
});
}
});
, а вот второй.
router.get("/sumResult", function(req, res) {
var itemPrice = req.params.itemPrice;
var isSolved = function(itemPrice, callback){
console.log("in the aggregation: ", itemPrice);
Idea.aggregate([
//var isSolved = User.aggregate([
{$group: {
_id: null,
totalPrice: { $sum: "$itemPrice" },
total: {$sum: 1}
}}
],
function(err, idea){
console.log("this is the result: ", idea); // logs a result if the there is one, and [] if there is no result.
callback(err, idea); // <<=== call callback here to return
});
};
isSolved(itemPrice, function(err, idea) {
if (err) {
return def.reject(err);
} else {
res.render('ideas/sumResult', {
idea:idea
});
}
});
});
Я изо всех сил стараюсь получить оба результата в одном, но не могу этого сделать, я новичок в узле.