1

I figured, when I insert into Mongo, Meteor's Fiber magic waits/blocks until the db acknowledged the write, but not until the observers are called. Is there a way to wait for them?

I'm inserting data in server code, and I have a Caching layer that observes added events on the Collection and casts ObjectModel instances from them:

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))

would print:

Inserted ... - undefined, {...}

i.e. the CachingService didn't receive the insert yet, but the database did.

TeNNoX
  • 1,899
  • 3
  • 16
  • 27

1 Answers1

-1

Maybe try injecting the observe property on the collection cursor not the instance (i.e 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' });
})

Other helpful questions: Meteor collection observe changes rightly cursor.observe({added}) behavior in Meteor

Harry Adel
  • 1,238
  • 11
  • 16
  • We're already observing the cursor. It's the only way meteor supports doing it. The other questions you linked deal with observation events for already existing models - but we already handle that. – TeNNoX Nov 18 '19 at 11:55