I'm getting started with NodeJS and i'm blocked with this problem for more then two days now! if someone can help i will be very thankfull!
i'm trying to get a query result, then map all results and apply another uery then render both query results. here is my code :
app.get('/home/', function(req, res) { 
let sessionUser = req.session.user;
if(sessionUser) {
    user.findMemosByUserId(sessionUser.id, function(result) {
        result.forEach(function(memo){
            //get users having acces to each memo
            user.findUsersByMemoId(memo.id, function(result2){
                var obj = {};
                obj[memo.id] = [];
                result2.forEach(function(res){
                    obj[memo.id].push(res.username);
                });
                req.session.sharings.push(obj);
                console.log(req.session.sharings); //it shows this
            });
        });
        console.log(req.session.sharings); //it shows this first
        res.render('home.ejs', {memos: result, sharingsData: req.session.sharings});
    });
    return;
}
res.redirect('/');})
then the problem is that it's rendering the page before looping and getting each memo users (while req.session.sharings is still empty)
login result is :
[ ]    
[{ },{ }] // with my data 
Anyone has any idea of how i can force:
 res.render('home.ejs', {memos: result, sharingsData: req.session.sharings});   
to wait until the end of for each loop !!!!
 
     
    