猫鼬查询时的 Javascript 变量范围

Javascript variable scope when mongoose query

本文关键字:Javascript 变量 范围 查询      更新时间:2023-09-26

我正在使用node.js,mongoose和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 }));
        });

我的房间阵列有问题。当我删除"Room.aggregate"查询时,工作正常(所有房间都可以),但是当我使用聚合时,返回函数给了我空空间。

我已经尝试从"var rooms=[];"中删除var

Room.aggregate 是异步函数,如果你想迭代异步函数,你可以使用异步库,像这样

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
        }));
    });
});