2016-08-02 1 views
1

を、これは私のアーキテクチャです:NodeJS - サービスはモンゴ使用することはできません - 私の問題を説明する前に

1 - server is running, getting request and storing data 
2 - a service - called process_runner.js - is running on a 2nd terminal 

サービスのポイントは、いくつかの機能を実行するために私のデータベースからデータをフェッチすることです。

このサービスされていますprocess_runner.js

// all needed requires 
/// ... 
// 


mongoose.connect(config.database); 

var db = mongoose.connection; 

db.on('error', console.error.bind(console, 'Error connecting to MongoDB:')); 
db.once('open', function() { 
    console.log("Connected to MongoDB"); 
    try { 
    run(); 
    } catch (e) { 
    console.log (e); 
    } 

}); 


//... 

var run = function() { 

console.log("Start processes manager"); 

var taken = false; 
while(true) { 
    console.log ("iteration") 

    if (taken == false) { 
    taken = true; 

    console.log("go"); 
    // Then I want to get my capacities 
    // when the call below is done, nothing appends and the loop continues 

    Capacity.find({} , function(err, capacities) { 
     console.log ("OK CONTINUE"); 
     // ... 
     // next of the events 
    }); 
... }... 

(ループがsleep(1)を持っている)

が、これが出力されますので、

Connected to MongoDB 
Start processes manager 
iteration 
go 
iteration 
iteration 
iteration 
... 

、私は必要な 'GO' のメッセージの後に「OK CONTINUE」メッセージを受信すると、残りのコードが実行されます。

しかしCapacity.find({} , function(err, capacities) {.... が行われたときに、何も追加しないとループが(errで何を)続けない

任意のアイデア?

+0

単純に無限ループを削除し、正常に実行されると、同じ関数を呼び出しますか?なぜあなたはそれが必要なのですか? –

+0

ポイントは、サービスtuがバックグラウンドで実行できるようにすることです 私はループなしで試してみます – F4Ke

+1

実行が終了したときに同じ機能を呼び出すだけです –

答えて

1

ここでの問題は、ループwhile(true)です。 Node.jsはシングルスレッドであるため、実行ループをブロックしているだけで、データベース呼び出しを実行できません。あなたは `しばらく(真)`ループを削除する場合はどう

var run = function() { 
    Capacity.find({} , function(err, capacities) { 
    //do stuff 
    return run(); 
    }); 
} 
関連する問題