Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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 NodeJS异步函数。怎样做A和B,然后做C?_Javascript_Node.js_Asynchronous_Mongodb Query - Fatal编程技术网

Javascript NodeJS异步函数。怎样做A和B,然后做C?

Javascript NodeJS异步函数。怎样做A和B,然后做C?,javascript,node.js,asynchronous,mongodb-query,Javascript,Node.js,Asynchronous,Mongodb Query,我编写了以下代码,并在mongo控制台上成功地进行了测试 var up_results = db.upParts.find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}},{"_id":1, "features.properties.partID":1,"features.properties.connectedTo":1}).toArray(); var dow

我编写了以下代码,并在mongo控制台上成功地进行了测试

var up_results = db.upParts.find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}},{"_id":1, "features.properties.partID":1,"features.properties.connectedTo":1}).toArray();
var down_results = db.downParts.find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [-80, 30]}}}},{"_id":1, "features.properties.partID":1}).toArray();
var part_results = [];

for (i = 0; i < up_results.length; i++) {
    for (j = 0; j < up_results[i].features[0].properties.connectedTo.length; j++) {
        for (k = 0; k < down_results.length; k++) {
            if (down_results[k]._id == up_results[i].features[0].properties.connectedTo[j]) {
                part_results.push(db.parts.find({_id:{$in:[ObjectId(up_results[i].features[0].properties.partID)]}}));
            }
        }
    }
}

parts_results.length;
如何告诉geoqueries()upParts和downParts find查询已完成?

尝试使用Promissions

是最好的资源之一

你可以做一连串的承诺。 比如:

var promises = [];

var deferred = q.defer();
promises.push(deferred.promise);

self.db.collection('upParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}}, {"_id": 1, "features.properties.partID": 1, "features.properties.connectedTo": 1}).toArray(function (err, document) {
    up_results = document;
    deferred.resolve();
});

var deferred_i = q.defer();
promises.push(deferred_i.promise);
self.db.collection('downParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [-80, 30]}}}},{"_id":1, "features.properties.partID":1}).toArray(function(err, document2) {
    down_results = document2;
    deferred_i.resolve();
});

q.all(promises)
.then(function() {
    callback();
});

谢谢Chris,我看过promises和async,async也很好
var promises = [];

var deferred = q.defer();
promises.push(deferred.promise);

self.db.collection('upParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}}, {"_id": 1, "features.properties.partID": 1, "features.properties.connectedTo": 1}).toArray(function (err, document) {
    up_results = document;
    deferred.resolve();
});

var deferred_i = q.defer();
promises.push(deferred_i.promise);
self.db.collection('downParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [-80, 30]}}}},{"_id":1, "features.properties.partID":1}).toArray(function(err, document2) {
    down_results = document2;
    deferred_i.resolve();
});

q.all(promises)
.then(function() {
    callback();
});