Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor JS : Client not getting data from Mongo DB

I have started learning MeteorJS and made a sample app. I have a collection in mongoDB and I am trying to see that collection in client Here is my server Code(file is in /libs)

newColl=new Meteor.Collection("newColl");
if(Meteor.isServer){
  Meteor.publish('newCollectionData', function(){
     console.log(newColl.find().fetch());
    return newColl.find();
  });
}

Here is My client Code(file is in /client)

  Meteor.subscribe("newCollectionData");
//console.log(newColl.find());
console.log(newColl.find().fetch());
var data= newColl.find().fetch();
console.log(data);

The log in server prints the data correctly but the log on client prints an empty array. PS: I have removed auto publish, but with it also it was giving same result. Where am I going Wrong?

like image 824
user1551574 Avatar asked Feb 12 '23 20:02

user1551574


1 Answers

Cursor.fetch() returns immediately with data currently available. If no data are available on the client in the time of call, it returns nothing.

It is reactive source, so just try to call it in Tracker.autorun and it will be recomputed when the reactive source changes.

Tracker.autorun(function () {
  console.log(newColl.find().fetch());
});
like image 137
Krab Avatar answered Feb 14 '23 10:02

Krab