【问题标题】:NodeJS asynchronous functions. How to do A and B then C?NodeJS 异步函数。 A和B然后C怎么做?
【发布时间】:2014-07-28 19:52:12
【问题描述】:

我写了如下代码,并在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;

我现在正在尝试在 nodejs 中实现它......但我认为我没有做对

我是这样开始的:

var up_results = null;
var down_results = null;
var part_results = [];

function geoqueries(callback) {
            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;
            });
            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;
            });
            callback();
        }

function dosomething() {
    ...do something with up_results and down_results
}

geoqueries(dosomething);

我如何告诉 geoqueries() upParts 和 downParts 查找查询已完成?

【问题讨论】:

标签: javascript node.js asynchronous mongodb-query


【解决方案1】:

尝试使用 Promises。

http://howtonode.org/promises 是最好的资源之一。

你可以做一系列的承诺。 比如:

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,我查看了 Promise 和 async,async 也很好用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多