Nastyo
Nastyo

Reputation: 199

Sails.js Async request

I would like to count how many entreprise are in some category but I'm stuck with the asynchrone concept.

Here's what I already have:

Category.getall(function(err, cat){

    if(err) return res.negotiate(err);

    catIds = []

    for( var iCat in cat){

        catIds.push(cat[iCat].id)

        // and here I would like do something like

        Entreprise.count({category_id: cat[iCat].id}, function(err, nbr){

            categoriesOUT.push({categorie: cat, entreprise_number: nbr })

            // I know that i can not do it but it's just to help to understand the logic I would like to have.
            if(cat.length==iCat){
                return res.json({categories: categoriesOUT})
            }

        })

    }

})

Upvotes: 0

Views: 429

Answers (2)

Nastyo
Nastyo

Reputation: 199

Here's how I finaly get it only with MySQL request as suggered by @arbuthnott (The category field is call domaine here)

    Domaine.getall(function(err, domaines){

        if(err){return res.negotiate(err)}

        var domNames = {}, domContain = {}, domOut = [];

        Entreprise.query('SELECT domaine_id, COUNT(*) FROM entreprise GROUP BY domaine_id', function(err, entreprises){
            if(err){return res.negotiate(err)}

            entreprises = JSON.parse(JSON.stringify(entreprises));

            for(var ent of entreprises){

                domContain[ent['domaine_id']] = ent['COUNT(*)'];
            }

            for(var iDom in domaines){

                var countAdded = false;

                for(var dc in domContain){                      

                    if(dc==domaines[iDom].id) {
                        domaines[iDom].entreprises_count = domContain[dc];
                        countAdded = true;
                    }

                }

                if(!countAdded) domaines[iDom].entreprises_count = 0;

            }

            res.json({domaines:domaines})

        })

    })

Upvotes: 0

arbuthnott
arbuthnott

Reputation: 3819

There are a couple of ways to handle this. One would be to bring in a promise library like Q. Another would be a single database call that can count up enterprise objects grouped by category_id... however, I think that would go beyond Waterline's normal queries, you would have to use .query or .native or something.

The easiest quick fix for you is to just keep a counter of how many results you have handled. You may get tired of this approach after using it a couple of times, but it would look something like this:

Category.getall(function(err, cat){
    if(err) { return res.negotiate(err); }
    var catIds = [], categoriesOut = [], processedCategories = 0;
    for( var iCat in cat){
        catIds.push(cat[iCat].id)

        Entreprise.count({category_id: cat[iCat].id}, function(err, nbr) {
            if (err) {
                categoriesOUT.push({categorie: cat, entreprise_number: 0});
            } else {
                categoriesOUT.push({categorie: cat, entreprise_number: nbr });
            }
            processedCategories += 1;
            if (processedCategories >= cat.length) {
                return res.json({categories: categoriesOUT});
            }
        });
    }
});

Upvotes: 1

Related Questions