Tuesday, 6 June 2017

Sails Query Returns Nothing After count() Query

The following returns empty results when executed. It's fine without the Count query, but even still, the correct values are returned but the response is always empty. Am I having a race condition issue?

module.exports = {
getSites: function (req, res) {
    var sites = [];
    Site.find({ company: req.session.companyID }).populate('locations').exec(function afterFind(err, results) {

        for (index in results) {

            var siteObj = {};

            siteObj = results[index];


            sails.models['site_users__user_sites'].count({ user_sites: results[index].id }).exec(function found(err, counted) {
                if (err) {
                    sails.log.error(err);
                }
                sails.log.debug(counted); // Prints correct number
                siteObj['user_count'] = counted;
                sails.log.debug(siteObj['user_count']); // Shows value is present
                sites.push(siteObj);
            });
        }
    });

    sails.log.debug(sites); // Nothing
    return res.json(sites); // Nothing
},}



via Ben Brookes

No comments:

Post a Comment