在Mongo插入Meteor服务器后如何等待观察者调用

我认为,当我插入Mongo时,Meteor的Fiber Magic会一直等待/阻止,直到db确认写入为止,但直到调用观察者时才等待。有办法等他们吗?

我正在将数据插入服务器代码中,并且具有一个缓存层,该层可以观察Collection上添加的事件并从中强制转换ObjectModel实例:

class CachingService {
  attachObservers() {
    this.collection.observe({
      added: models => this.added(models)
    })
  }
}
const id = Placements.insert({...})
console.log(`Inserted ${id} -`,CachingServices.Placements.getByID(id),Placements.getByID(id,false))

将打印:

  

已插入...-未定义,{...}

即CachingService尚未收到插入内容,但数据库收到了。

dasiy123zs 回答:在Mongo插入Meteor服务器后如何等待观察者调用

也许尝试将observe属性注入集合光标而不是实例(即testCollection.find().observe)上。


Meteor.startup(() => {

  const testCollection = new Mongo.Collection("testCollection");

  testCollection.find().observe({
  added: function(document) {
    console.log("groups observe added value function");
    console.log(document);
  }
});

  testCollection.insert({ foo: 'bar' });
})

其他有用的问题: Meteor collection observe changes rightly cursor.observe({added}) behavior in Meteor

本文链接:https://www.f2er.com/3118249.html

大家都在问