0
votes

I work on meteor with mongodb. I am trying to get a collection from database. I can insert data without any problem in this collection from meteor, but when I try to find, it doesn't works. My collection is 'first'.

Server side:

Meteor.publish('first', function(){
  return first.find();
});

Client side:

var datacollab = first.find({"Mois":"Mars"});
console.log("collab: " + datacollab);

When I make this command line in mongo shell, it works fine. I already try to change my request with findOne, or put .fetch at the end.

1
You did not specify whether you subscribe or not? - ghybs
Yes I did it in client side: Meteor.subscribe('first'); - Benjamin Lucidarme
Are you waiting for the subscription to be ready / for data to arrive? - ghybs
I guess no, because I do first.find in Template.mytemplate.rendered. But if I do it in .event, it works. How can I wait for subscription to be ready in .rendered?? I need to load my data in .rendered... - Benjamin Lucidarme

1 Answers

1
votes

If you need your code to be in your Template.myTemplate.onRendered hook, then you have several options:

  • Use a Tracker.autorun that will be automatically re-executed when your DB query / cursor returns new data.
  • Use the onReady callback of the subscription (assuming you subscribe either when template is created or rendered). Your callback will be executed when the client has received a first full snapshot of the server publication.