Wednesday, 15 March 2017

Get mongoose results in variable outside scope

I am new to nodejs and I am trying to do a basical website.

I use a mongodb database and I use mongoose and express.

Here is my problem :

I have a route like this:

app.get('/users', function(req, res) {
    res.render('account.ejs', {
      userlist: getUsers()
    });
});

And a function like this:

var User            = require('../app/models/user');

module.exports = {
  getUsers: function()
  {
    User.find(null, function (err, userlist) {
      if (err) { throw err; }
      console.log(userlist);
    });
   return (userlist);
  }
};

I know that the function for the query is asynchronous, but what is the simple way to return my json with my userlist ? He dont know the variable userlist in this situation.



via bluesgate

No comments:

Post a Comment