0

mongodb からデータを取得する node.js および Express で実行されている Web サーバーがあります。mongodb ではコレクションが動的に作成され、新しく作成されたコレクションの名前は 1 つのメタデータ コレクション「プロジェクト」に格納されます。私の要件は、最初にメタデータ コレクションを反復してコレクション名を取得し、次に各コレクション内に入って、いくつかの条件に基づいて複数のクエリを実行することです。私のコレクション メタデータは動的であるため、 for loop を使用して実行しようとしました。しかし、それは間違ったデータを与えています。シーケンスを実行していません。ループ実行を終了する前に、値を返します。ノードコアモジュールのみを使用して node.js で順次実行を実行する方法 (async のような他のライブラリではありません);

exports.projectCount = function (req, res) {
    var mongo = require("mongodb"),
        Server = mongo.Server,
        Db = mongo.Db;

    var server = new Server("localhost", 27017, {
        auto_reconnect: true
    });

    var db = new Db("test", server);
   // global JSON object to store manipulated data 
    var projectDetail = {
        projectCount: 0,
        projectPercent: 0
    };
    var totalProject = 0;
    db.open(function (err, collection) {
        //metadata collection 
        collection = db.collection("project");
        collection.find().toArray(function (err, result) {
            // Length of metadata  collection 
            projectDetail.projectCount = result.length;
            var count = 0;
            //iterate through each of the array which is the name of collection 
            result.forEach(function (item) {
                //change collection  object  to new collection
                collection = db.collection(item.keyParameter.wbsName);
                // Perform first query based on some condition 
                collection.find({
                    $where: "this.status == 'Created'"
                }).toArray(function (err, result) {
                    // based on result of query one increment the value of count 
                    count += result.lenght;
                    // Perform second query based on some condition 
                    collection.find({
                        $where: "this.status=='Completed'"
                    }).toArray(function (err, result) {
                        count += result.length;
                    });
                });
            });
            // it is returning the value without finishing the above manipulation
            // not waiting for above callback and value of count is coming zero .
            res.render('index', {
                projectDetail: projectDetail.projectCount, 
                count: count
            });

        });
    });
};
4

3 に答える 3

9

複数の非同期関数を順番に呼び出したい場合は、最初の関数を呼び出し、そのコールバックで次の関数を呼び出すというようにします。コードは次のようになります。

asyncFunction1(args, function () {
  asyncFunction2(args, function () {
    asyncFunction3(args, function () {
      // ...
    })
  })
});

このアプローチを使用すると、保守が困難な見苦しいコードになる可能性があります。

async.jsnode-fibersを使用するなど、コールバックをネストせずに同じ機能を実現するさまざまな方法があります。


node.js を使用してそれを行う方法は次のEventEmitterとおりです。

var events = require('events');
var EventEmitter = events.EventEmitter;

var flowController = new EventEmitter();

flowController.on('start', function (start_args) {
  asyncFunction1(args, function () {
    flowController.emit('2', next_function_args);
  });
});

flowController.on('2', function (args_coming_from_1) {
  asyncFunction2(args, function () {
    flowController.emit('3', next_function_args);
  });
});

flowController.on('3', function (args_coming_from_2) {
  asyncFunction3(args, function () {
    // ...
  });
});

flowController.emit('start', start_args);

ループ シミュレーションの例:

var events = require('events');
var EventEmitter = events.EventEmitter;

var flowController = new EventEmitter();

var items = ['1', '2', '3'];

flowController.on('doWork', function (i) {
  if (i >= items.length) {
    flowController.emit('finished');
    return;
  }

  asyncFunction(item[i], function () {
    flowController.emit('doWork', i + 1);
  });

});

flowController.on('finished', function () {
  console.log('finished');
});

flowController.emit('doWork', 0);
于 2013-10-04T07:33:00.080 に答える
0

上記の回答のリソースによると、反復時にコールバックをネストし、最後の反復の場合にのみコールバックを呼び出すと、問題が解決します。

于 2014-02-11T20:33:27.860 に答える
0

コールバックまたはプロミス、またはフロー制御ライブラリを使用します。これらのアプローチの少なくとも 1 つを理解せずにノードでサーバーをプログラミングすることはできません。正直なところ、中途半端なノード プログラマーはすべて、3 つすべてを完全に理解しています (いくつかの異なるフロー制御ライブラリを含む)。

これは、スタックオーバーフローで他の誰かがコード化した回答を取得してから先に進むというものではありません。これは、日常的に何度も何度も出てくるだけなので、一般的に行って勉強し、学ばなければならない基本的なことです.

http://howtonode.org/control-flow

http://callbackhell.com/

于 2013-10-04T07:12:11.943 に答える