为什么我的闭包没有及时执行每次循环迭代?

时间:2015-02-14 11:39:01

标签: node.js mongodb mongoose

bus_near_stops以空数组开头。在对数据库的异步调用中,它应该被填充。然后在调用完成后,我想使用它内部的数据。当我运行此代码时,bus_near_stops的最终控制台日志在内部数据库调用之前执行,即使我在闭包内部进行了循环调用。根据{{​​3}}帖子,关闭应该有效,但这里对我没有任何作用。

var buses_near_stops = [];
buses_near_stops.test = "TEST";


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

         doc.forEach(function(j,k) {
         //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
             (function(buses_near_stops) {
                   Stop.findOne({coordinates: { $near : j.coordinates, $maxDistance: .0001}
              }, function(err, stop){
                    if(err){return next(err);}

                        console.log('stop found ' + j.service_name + " " + JSON.stringify(stop));
                        // 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 != null && 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
                                    console.log('test ' + buses_near_stops.test);
                                    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('length ' + buses_near_stops.length);
                                }
                            });

                        }
                    })}(buses_near_stops));

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

0 个答案:

没有答案