Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Doing an async call within a dynamicHelper (or some alternative) in Express 2.x

Doing an async call within a dynamicHelper (or some alternative) in Express 2.x

Problem

I'm trying to have a total Message count for a user's inbox displayed within my layout. I was thinking that I needed to use Express' dynamicHelpers to do this, but in Express

I'm trying the following to place the count within my session, which itself is put in a dynamicHelper accessible to the views. However, due to the asynchronous nature of these callbacks, session.unreadMessages is always undefined.

messageCount: function(req, res) {
    var Messages = require('../controllers/messages'),
            messages = new Messages(app.set('client'));

    if(req.session.type === 'company') {
        messages.getCompanyUnreadCount(req.session.uid, function(err, result) {
            req.session.unreadMessages = result[0].unread;
        });
    } else if(req.session.type === 'coder') {
        messages.getCoderUnreadCount(req.session.uid, function(err, result) {
            req.session.unreadMessages = result[0].unread;
        });                     
    }

    return;
}

Is there another or better way to perform this task?

It should be noted that req.session.unreadMessages is defined (at least within that callback), but undefined when session is called using the helper.

Problem courtesy of: Josh Smith

Solution

Not sure, it it would be a 'best way', but I'm used to using a filter (or a so called middleware) to load data before it reaches the actual destiny, like in:

filters.setReqView = function(req,res,next) {
    req.viewVars = {
        crumb: [],
        flash_notice: augument,
    }

    somethingAsync(function(err,data){
        req.viewVars.someData = data
        next()
    })
}

app.all('*', filters.setReqView )


// then on my request:
...
res.render('auth/win', req.viewVars )

Refactoring your code you would have:

app.all('*', function(req, res, next) {
    if(req.session && req.session.type){
        var Messages = require('./controllers/messages'),
            messages = new Messages(app.set('client'));

    if(req.session.type === 'company') {
        messages.getCompanyUnreadCount(req.session.uid, function(err, result) {
            req.session.messageCount = result[0].unread;
            next();
        });
    } else if(req.session.type === 'coder') {
        messages.getCoderUnreadCount(req.session.uid, function(err, result) {
            req.session.messageCount = result[0].unread;
            next();
        });                     
    }
    } else {
        next()
    }
});
Solution courtesy of: Fabiano PS

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Doing an async call within a dynamicHelper (or some alternative) in Express 2.x

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×