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?
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});
});
});
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 });
}
);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With