0

I'm working with node.js, mongoose and foursquare API.

            foursquare.getVenues(params, function(err, venues) {
            if(err) return res.json(JSON.stringify({status: 'error', returnData: err}));

            // variable initialization
            var rooms = [];
            var vanueItem;

            // iterate foursquare return list (venue item)
            venues.response.venues.forEach(function(item) { 
                Room.aggregate(
                    [
                    { "$group": { 
                            "_id": '$mobileUser.genderType', 
                            "genderTypeCount": { "$sum": 1 }
                        }}
                    ],
                    function(err,result) {
                    if(err) return res.json(JSON.stringify({status: 'error', returnData: err}));

                    // build it to return after
                    vanueItem = 
                        {
                            id: item.id,
                            name: item.name,
                            description: item.description,
                            contact: item.contact.formattedPhone,
                            lat: item.location.lat,
                            lng: item.location.lng,
                            distance: item.location.distance,
                            city: item.location.city
                        };  

                        // insert it into venue array
                        rooms.push(vanueItem);  
                     }
                );
            });

            return res.json(JSON.stringify({ status: 'success', returnData: rooms }));
        });

I'm having a problem with rooms array. When I remove the 'Room.aggregate' query, works fine (all rooms was ok), but when I use the aggregate, the return function gives me empty room.

I already tried remove var from 'var rooms = [];'

0

1 Answer 1

1

Room.aggregate is asynchronous function, if you want iterate over asynchronous function you can use async library, like this

var async = require('async');

foursquare.getVenues(params, function(err, venues) {

    if (err) return res.json(JSON.stringify({
        status: 'error',
        returnData: err
    }));

    var rooms = [];
    var vanueItem;

    async.each(venues.response.venues, function (item, next) {
        Room.aggregate(
            [{
                "$group": {
                    "_id": '$mobileUser.genderType',
                    "genderTypeCount": {
                        "$sum": 1
                    }
                }
            }],
            function(err, result) {
                if (err) {
                    return next(err);
                }

                // build it to return after
                vanueItem = {
                    id: item.id,
                    name: item.name,
                    description: item.description,
                    contact: item.contact.formattedPhone,
                    lat: item.location.lat,
                    lng: item.location.lng,
                    distance: item.location.distance,
                    city: item.location.city
                };

                rooms.push(vanueItem);

                next(null);
            }
        );

    }, function (err) {
        if (err) {
            return res.json(JSON.stringify({
                status: 'error',
                returnData: err
            }));
        }          

        return res.json(JSON.stringify({
            status: 'success',
            returnData: rooms
        }));
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, its worked fine! But i got a question... Why i need to code "next(error)" ?
@Ren4n I've updated example.., next is callback function that called once when async query has completed, next(err) - this code means that if async query returns error, in last callback we get error message. next(null) - needs to go to next item in array when query has completed... github.com/caolan/async#eacharr-iterator-callback

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.