L.T
L.T

Reputation: 2589

Use setTimeout for query Collection in Meteor

I defined a Collection in the model.js like this:

People = new Meteor.Collection("people");

Here's the code in main.js:

function test2(){
  console.log(JSON.stringify(People.find().fetch()));
  setTimeout(test2,5000)
}
if (Meteor.isServer) {
  if(People.find().fetch().length === 0){
    var tom = {name:"Tom",age:18};
    People.insert(tom);
  }

  Meteor.startup(function () {
    test2();
  });
}

Here are the errors I get:

enter image description here

I want to Meteor execute some CRUD on the Collection automatically at set intervals. So I'm using setTimeOut, but it seems to be difficult.

Any idea about what I am doing wrong?

Upvotes: 1

Views: 2609

Answers (1)

Tarang
Tarang

Reputation: 75955

Use Meteor.setTimeout instead:

Meteor.setTimeout(test2,5000)

On the server meteor code is run using fibers to let your write synchronous code, javascript's timeout has to let its callback fire in a fiber too, especially if it contains meteor code.

Upvotes: 5

Related Questions