2016-10-12 14 views
0

を更新し、私は少しのアプリを作ったが、私は、例えば、サーバ側流星/ MongoDBを見つけて、私はちょうど流星を学んでいる

find()update()コレクションに問題があります。

if (Meteor.isServer) { 

    function getCollection1(){ 
     return collection_1.find({}).fetch({}); 
    } 
    .... 
    Meteor.methods({ 

     start: function(id) { 

      datas = getCollection1(); 
      Collection2.update({_id:id}, {$set: {datas: datas}}); //sometimes it's ok(3/4) 

     } 
     ... 

    } 

をまたは私が待っているときにエラーが発生しました

if (Meteor.isServer) { 

    async function getCollection1(){ 
     return await collection_1.find({}).fetch({}); 
    } 
    .... 
    Meteor.methods({ 

     start: function(id) { 

      getCollection1().then(function(datas){ 
       Rooms.update({_id: id}, {$set: {datas: datas}},function(err){ 
        console.log(err);//error: Meteor code must always run within a fiber 
       }); 
      }); 
     } 
     ... 

    } 

どうしましたか?あなたが提供されたコードに基づいてFiber()

if (Meteor.isServer) { 
    Fiber = Npm.require('fibers')  
     function getCollection1(){ 
      return collection1.find({}).fetch({}); 
     } 
     function setCollection2(id){ 
     Fiber(function() { 
      datas = getCollection1();    
       collection2.update({_id: id}, {$set: {datas: datas}},function(err){ 
        console.log(err); 
       }); 
     }).run(); 
     } 
     .... 
     Meteor.methods({ 

      start: function(id) { 

       setCollection2(id); 

      } 
     ... 

     } 
+0

本当にわからないが、これは役立つかもしれない:HTTP ://stackoverflow.com/questions/10192938/meteor-code-must-always-run-within-a-fiber-when-calling-collection-insert-on-s –

+0

あなたはあなたがfind/updateを同期モードでコールバックを渡さないでください。 – MasterAM

+0

ファイバーには別の方法がありますか? – Dioux

答えて

-1

でうまく動作するようです

EDIT

、私の推測では、最初の例については、「Collection2.updateは」前に行われていることですGetCollection1()が完了しました。これが実際に当てはまる場合は、コレクションに登録するときにクライアント側で、サブスクリプションが完了するのを待つ方法があることを確認してください。例えば

あなたがあなたの最初に受信したエラーを投稿していなかったとして、「開始()が」と呼ばれている...

const handle = Meteor.subscribe('collection_1'); 
    if (handle.ready()) { 
    Meteor.call('start'); 
} else { 
    const allusers = Meteor.users.find().fetch(); 
    onData(null, {allusers}); 
} 

繰り返しますが、これは私の最高の推測である、クライアント側でこのような何かコードチャンクと私はあなたがこれを実装しようとした2番目のあなたのために話すことはできません。

+0

"GetCollection1()が完了する前に" Collection2.update "が実行されています"はい問題だった – Dioux

+0

サブスクリプションはありません。これはすべてサーバー側です。 –

0

async/awaitバージョンでは、Fiberを使用する必要はありません。代わりに、あなたはこの流星機能できます

より理解しやすい説明のために
// ...  
getCollection1().then(Meteor.bindEnvironment(function(datas){ 
    Rooms.update(// ...); 
})); 
// ... 

は、あなたがこの動画に相談できます:Meteor.bindEnvironmentはそれを動作させるために

What is Meteor.bindEnvironment?

関連する問題