Victor Ferreira
Victor Ferreira

Reputation: 6449

Querying Mongo returns empty array in Template.foo.onCreate in Meteor app

I get this code in my Meteor project, in a client/main.js file

Template.panel.onCreated(function loginOnCreated() {
  var profile = Session.get('profile');

  this.myvar = new ReactiveVar(User.find({}).fetch());
});

And the result of User.find({}) is empty. If I Query this anywhere else (including meteor mongo) I get an Array of users.

So I wonder if it is a problem with the fact that this code is running in client side. In this same file I get this query working in other places, but probably in the server context.

How can I populate this ReactiveVar with the Mongo result as soon as the Template/page is loaded?

If I do something like in Meteor.startup() at Server side:

console.log(User.find({}).count());

It gives me the correct number of Users. Immediately.

@edit

If I just add a setTimeout of a few seconds (it can't be jsut 1 second, it needs a longet time), it works in this very same place.

Template.panel.onCreated(function loginOnCreated() {
//...
setTimeout(function(){
    template.timeline.set(User.find({}).fetch());
    console.log(timeline)
  },3000);
});

So, anyone knows why it takes so long to allow me to do this operation? Any workaround?

Upvotes: 0

Views: 204

Answers (1)

Shivani
Shivani

Reputation: 43

User.find({}).fetch() will give list of users on server side only.

You can probably write a meteor method for fetching the user list on server side and give it call using meteor.call.

In the callback function to this call you can assign the result to desired variable.

Upvotes: 0

Related Questions