为什么我的查询返回顶级结果,但嵌套时却没有?

时间:2015-02-11 19:23:18

标签: javascript node.js mongoose

在顶部的Location.find()中,doc有效并返回我想要的信息。但是,我想从下面的嵌套函数中找到所有位置。查询执行,但不返回任何文档。我知道这是真的,因为doc.forEach循环中没有任何内容运行,因为doc是一个空数组。数据库和所有模型都有效。我意识到我可能还没有正确实现所有嵌套,但是现在我只想弄清楚如何让查找正常工作

Location.find({}, 'service_name coordinates vehicle_id last_gps_fix', function(err, doc) {
    console.log('this is a doc ' + doc);
});

setInterval(function() {

    // ====== Live Bus Locations ====== //
    https.get("https://tfe-opendata.com/api/v1/vehicle_locations", function(res){
        //Clear the old data
        Location.remove({}, function(err) {
            if(err){return next(err);}
        });


        var body = '';
        res.on('data', function(chunk){
            body += chunk;

        });

        res.on('end', function() {
            console.log('end data');
            var json = JSON.parse(body);
            var doc = {};
            for(var i in json.vehicles) {
                var vehicleDoc = json.vehicles[i];

                var coordinates = [];
                coordinates.push(vehicleDoc.longitude);
                coordinates.push(vehicleDoc.latitude);
                vehicleDoc.coordinates = coordinates;
                delete vehicleDoc.latitude;
                delete vehicleDoc.longitude;

                //Convert unix timestamp to "HH:MM" so that it can be compared with departure times in Journeys or Timetables
                //12 Hour format
                var date = new Date(vehicleDoc.last_gps_fix * 1000);
                var hour = date.getHours();
                if(hour > 12)
                    hour = hour % 12;
                var hourString = String(hour);
                var minutes = date.getMinutes();
                var minutesString = String(minutes);
                if(minutes < 10)
                    minutesString = "0" + minutesString;
                var time = hourString + ":" + minutesString;
                vehicleDoc.time = time;

                var loc = new Location(vehicleDoc);

                (function() {
                    loc.save(function (err, post) {
                        if (err) {
                            return next(err);
                        }


                    });
                }());
            }

            var buses_near_stops = [];


            // return these fields of each location document in the database
            Location.find({}, 'service_name coordinates vehicle_id last_gps_fix', function(err, doc) {
                console.log('location found ' + JSON.stringify(doc));
                if(err){return next(err);}
                console.log('no error');
                doc.forEach(function(j,k) {
                    console.log('for each');
                    //Find a stop that is near enough to each given bus that we can say the bus is 'at' that stop
                    //Making sure it returns 1 stop now because I don't know proper distance
                    Stop.findOne({
                        coordinates: { $near : j.coordinates, $maxDistance: .00001}
                    }, function(err, stop){
                        if(err){return next(err);}

                        console.log('stop found');
                        // service_name is null if bus is out of service (I believe)
                        if(stop !== null && j.service_name !== null) {
                            var service_name_of_bus = j.service_name;
                            console.log('service name of bus ' + service_name_of_bus);

                            // Find the service document associated with service_name_of_bus
                            var service_of_name = Service.findOne({name: service_name_of_bus}, function(err, service_of_name){
                                if(err){return next(err);}

                                // If the service has 'stop' on its route
                                if(service_of_name.routes[0].stops.indexOf(stop.stop_id) > -1) {
                                    console.log('stop found on service');
                                    // We have now found a bus that is stopped at a stop on its route
                                    buses_near_stops.push(
                                        {
                                            time: j.last_gps_fix,
                                            bus_coords: j.coordinates,
                                            stop_coords: stop.coordinates,
                                            vehicle_id: j.vehicle_id,
                                            stop_id: stop.stop_id,
                                            service_name: service_name_of_bus
                                        });
                                }
                            });

                        }
                    });

                });
            });

            console.log('buses near stops ' + JSON.stringify(buses_near_stops));


            //updateStats(buses_near_stops);
        });
    });



    // Iterating over all buses currently stopped at stops on their respective routes


}, 15000);

0 个答案:

没有答案