Pass data through to the view in Express
Solution 1
Node executes the query asynchronously. That is, the the result of the query is not returned immediately. You have to wait untill the result is returned and callbacks are used to accomplish this.So, the render page call has to happen within the callback. Try modifying your function like this.
app.get('/dashboard', function(req, res) {
User.find({}, function(err, docs) {
console.log(docs);
});
User.find({
points: {
$exists: true
}
}, function(err, docs) {
if(err){
console.log(err);
//do error handling
}
//if no error, get the count and render it
var count = 0;
for (var i = 0; i < docs.length; i++) {
count += docs[i].points;
}
var totalpoints = count;
res.render('dashboard', {
title: 'Dashboard',
user: req.user,
totalpoints: totalpoints});
});
});
Solution 2
Node.js is asynchronous in nature, res.render
will be executed before you get data from the mongoose. Try the following code.
app.get('/dashboard', function(req, res) {
User.find({}, function(err, docs) {
console.log(docs);
});
User.aggregate({ $group: {
_id: null,
count: { $sum: "$points" }
}}, function(err, docs) {
if(err){
console.log(err);
//do error handling
}
else
res.render('dashboard', {
title: 'Dashboard',
user: req.user,
totalpoints: docs.count });
}
);
});
ServerSideSkittles
Updated on July 25, 2022Comments
-
ServerSideSkittles almost 2 years
I am trying to pass through the result of a query through to my view in Express. The query is made using mongodb which counts the total points of the collective users.
When I try to pass the count through as a variable, I get
ReferenceError: /Sites/test/views/dashboard.ejs:76
which refers to <%= totalpoints %> in my ejs view. Below is my code in app.js
app.get('/dashboard', function(req, res) { User.find({}, function(err, docs) { console.log(docs); }); User.find({ points: { $exists: true } }, function(err, docs) { var count = 0; for (var i = 0; i < docs.length; i++) { count += docs[i].points; } return count; console.log('The total # of points is: ', count); }); var totalpoints = count; res.render('dashboard', { title: 'Dashboard', user: req.user, totalpoints: totalpoints }); });
Any ideas how I can pass the query result through?
-
Vishnu over 8 yearsI doubt whether
res.render
waits till for loop is completed -
Vishnu over 8 years@ServerSideSkittles Use
User.aggregate
for finding the summation of points instead of adding in a for loop. mongoosejs.com/docs/api.html#aggregate-js -
robertklep over 8 years@Mahesh
for
loops are synchronous, so yes,res.render()
will "wait".