1

I just start to use node.js with express and mongoose, and I have a stupid question...

Somewhere in my routes.js file, I have the following section :

// DASHBOARD SECTION, GET MY GROUPS
    app.get('/dashboard', isLoggedIn, function(req, res) {
        var Group = require('../app/models/group'); //adding mongoose Schema
        Group.find({"groupDetails.userId" : req.user._id})
        .exec(function(err, myGroups) {
            if (err)
                res.send(err);
            var myGroups = myGroups;

            //console.log("myGroups: " + myGroups); // check with "heroku logs"

            res.render('dashboard.ejs', {
                user : req.user,
                myGroups : myGroups
            });
        });
    });

This code works. When someone browse the dashboard page, I receive "myGroups" which is an array with all the groups for the current logged in user.

Now, here is my question :

Actually when someone browse the dashboard page, I would like to make a second query (based on the exact same pattern) to get all groups and all files for the current logged in user.

Then I will send "user", "myGroups" and "myFiles" to the dashboard page. How can I do that ?

I tried several things with no result so far... I think I'm a little bit lost in node.js callback functions :D

Thanks a lot for your help.

1 Answer 1

2

You have two options here:

1) deal with callback hell (callback inside callback inside...) to retrieve 3 sets of data. This way is least elegant and efficient

2) Use a library that will do the job asynchronously and have one callback when all the data is retrieved, you can use async library which is just awesome. In this case you will have just one callback in which you can access all the data you have fetched.

Here's what you can do with async in your case:

var async = require('async');

..........

app.get('/dashboard', isLoggedIn, function(req, res) {
    var Group = require('../app/models/group'); //adding mongoose Schema
    var User = require('../app/models/user'); //adding mongoose Schema
    var Files = require('../app/models/files'); //adding mongoose Schema
    async.parallel({
       groups: function(callback){
           Group.find(...).exec(callback);
       },
       users: function(callback){
           Users.find(...).exec(callback);
       },
       files: function(callback){
           Files.find(...).exec(callback);               
       }
    }, function(err, results) {
       if (err)
            res.send(err);

       var groups = results.groups;
       var users = results.users;
       var files = results.files;

       res.render('dashboard.ejs', {
            user : req.user,
            myGroups : groups,
            users: users,
            files: files
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.