Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data through to the view in Express

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?

like image 399
ServerSideSkittles Avatar asked Aug 25 '15 04:08

ServerSideSkittles


2 Answers

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});
  });


});
like image 103
plbit Avatar answered Oct 01 '22 05:10

plbit


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 });
      }
  );

});
like image 33
Vishnu Avatar answered Oct 01 '22 05:10

Vishnu