2017-06-04 122 views
0

以下代碼在集合中填充某個信息時觸發客戶端。有時它不適用於智能手機,但在桌面版Chrome上運行良好。
在集合上調用findfindOne是否正確?不是過度殺人? THX流星雨收集觀測變化

'myMethod': function (age, health) { 
    Meteor.call('doStuff-that-will-change-the-document'); 
    myCollection.find().observeChanges({ // <===== find 
     changed: function (id, fields) { 
     let newDoc = myCollection.findOne({age: age}); // <==== another findOne 
     Meteor.users.update({_id: Meteor.user()._id}, {$unset: {'profile.taskInProgress': ''}}); 
     if (newDoc) { 
      if (fields.filed1) { 
      lib.dothis1(newDoc, health); 
      } else if (fields.field2) { 
      lib.dothat(newDoc, health); 
      } 
     } 
     } 
    }); 
    }, 

編輯

我需要的客戶端代碼火lib.dothis1lib.dothatfield1field2分別得到更新。

回答

0

如果你想趕上加什麼,只是使用added屬性 - see the docs

建立調用回調實況查詢時的查詢變化的結果。與觀察相反,observeChanges僅提供新舊結果集之間的差異,而不是所更改文檔的全部內容。

回調可以具有以下功能性質:

added(id, fields) or 

一個新的文檔中輸入的結果集。它具有指定的ID和字段 。字段包含文檔的所有字段,不包括 _id字段。新文檔在之前識別的文檔之前,或者在之前爲空的文檔之前。

比添加新文檔時會得到新的回調,這會比再次使用find更有效。

+0

我再次探討了這個問題,目前還不清楚'年齡'如何通知一個新的文檔,所以也許我誤解了這個問題 - 如果讓我知道,並澄清你希望用'findOne'達到什麼目的。 – kabanus