Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的查询在顶层返回结果,而在嵌套时不返回结果?_Javascript_Node.js_Mongoose - Fatal编程技术网

Javascript 为什么我的查询在顶层返回结果,而在嵌套时不返回结果?

Javascript 为什么我的查询在顶层返回结果,而在嵌套时不返回结果?,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,在顶部位置.find()中,doc是有效的,并返回我想要的信息。但是,我想从下面的嵌套函数中查找所有位置。执行查询,但不返回任何文档。我知道这是真的,因为doc.forEach循环中没有任何内容运行,而且doc是一个空数组。数据库和所有模型均有效。我意识到我可能还没有正确地实现所有的嵌套,但现在我只是想弄清楚如何让发现正常工作 Location.find({}, 'service_name coordinates vehicle_id last_gps_fix', function(err, d

在顶部位置.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);
Location.find({},'service\u name coordinates vehicle\u id last\u gps\u fix',函数(err,doc){
console.log('这是一个文档'+文档);
});
setInterval(函数(){
//=======现场公交车位置=======//
https.get(“https://tfe-opendata.com/api/v1/vehicle_locations“,函数(res){
//清除旧数据
Location.remove({},函数(err){
if(err){返回下一个(err);}
});
变量体=“”;
res.on('data',函数(块){
body+=块;
});
res.on('end',function(){
console.log(“结束数据”);
var json=json.parse(body);
var doc={};
for(json.vehicles中的变量i){
var vehicleDoc=json.vehicles[i];
变量坐标=[];
坐标推(车辆点经度);
坐标推(车辆中心纬度);
vehicleDoc.坐标=坐标;
删除vehicleDoc.latitude;
删除vehicleDoc.longitude;
//将unix时间戳转换为“HH:MM”,以便与旅程或时间表中的出发时间进行比较
//12小时格式
var日期=新日期(vehicleDoc.last\u gps\u fix*1000);
var hour=date.getHours();
如果(小时>12)
小时=小时%12;
var hourString=字符串(小时);
var minutes=date.getMinutes();
var minuteString=字符串(分钟);
如果(分钟<10)
minuteString=“0”+minuteString;
变量时间=小时字符串+“:”+分钟字符串;
vehicleDoc.time=时间;
var loc=新位置(车辆DOC);
(功能(){
位置保存(功能(错误、post){
如果(错误){
返回下一个(错误);
}
});
}());
}
var公交车靠近车站=[];
//返回数据库中每个位置文档的这些字段
查找({},'服务名称坐标车辆id最后一次全球定位系统定位',函数(err,doc){
log('location found'+JSON.stringify(doc));
if(err){返回下一个(err);}
console.log(“无错误”);
forEach文件(函数(j,k){
console.log('for each');
//找一个离每辆公交车足够近的站点,我们可以说公交车“在”那个站点
//确保它现在返回1站,因为我不知道正确的距离
住手,芬顿({
坐标:{$near:j.coordinates,$maxDistance:.00001}
},功能(错误,停止){
if(err){返回下一个(err);}
console.log('stop-found');
//如果总线停止运行,则服务\u名称为空(我相信)
if(停止!==null&&j.service\u name!==null){
变量服务\u总线的名称\u=j.服务\u名称;
console.log('总线的服务名称'+总线的服务名称');
//查找与\u总线的服务\u名称\u关联的服务文档
var service_of_name=service.findOne({name:service_name_of_bus},函数(err,service_of_name){
if(err){返回下一个(err);}
//如果服务在其路线上有“停止”
if(服务名称为[0].stops.indexOf(stop.stop\u id)>-1){
log(“在服务上发现停止”);
//我们现在发现一辆公共汽车停在其路线上的一个车站
靠近车站的公共汽车(
{
时间:j.last_gps_fix,
总线坐标:j坐标,
停止坐标:停止坐标,
车辆识别号:j.车辆识别号,
站住,站住,站住,
服务名称:总线的服务名称
});
}
});
}
});
});
});
console.log('bus near stops'+JSON.stringify(bus_near_stops));
//更新站点(公共汽车站附近);
});
});
//迭代当前停在各自路线站点的所有公交车
}, 15000);

您是否尝试了
查找.fetch()
?find将只返回mongo集合实例,而不是对象。请解释一下?如果使用
find()
,则只会获取集合实例,但如果使用
find().fetch())
您将获得集合中的对象
fetch
返回对象数组为什么find在外部调用中返回对象数组?此外,find没有返回内部的任何内容。甚至不是一个集合实例,它只是一个空数组。