Mycollection.count() =0 or mycollection isn't empty when I use db.mycollection.find()

Hey,

My code:

Mycollection = new Mongo.Collection(‘mycollection’);
Template.body.rendered = function () {
var collection = Mycollection.find();
console.log(collection);
};

This returns [] in the browser Javascript console but in my terminal, the command db.mycollection.find() dispalys to me all documents.

Make sure the client (browser) has the collection subscribed properly. You need to check if the subscription is actually ready before you can access the documents. Check out the guide

5 Likes

Thank you for the reply,

Actually, I am using autopublish in my app !! I think I don’t need to add and to subscribe to the publication, no ?

If you’re using autopublish that’s true. However, you do still need to wait for the subscription to be ready.

1 Like

Thank you for the reply again, I am so sorry I didn’t explain well !

When I try > Mycollection.find().count() in the console of the browser I get the real number of documents.
But when I wanna display it from my app.js file:


Mycollection = new Mongo.Collection(‘mycollection’);

Template.body.rendered = function () {
var mycollectionCursor = Mycollection.find();
console.log(mycollectionCursor.count());
};


Nothing displays to me in the browser :confounded:
I want to manipulate this cursor :frowning:

You did explain it alright the first time.

How many documents are in your collection? The issue is that when your Template renders, the subscription isn’t ready, therefore your cursor doesn’t report back anything.

My recommendation is to remove autopublish and work with publish/subscribe as documented in the guide.

Alternatively you could try to check DDP._allSubscriptionsReady()

Template.body.onRendered(function(){
 var self = this;
 self.autorun(function(){
  if( DDP._allSubscriptionsReady() ) {
   console.log( Mycollection.find().count() );
  }
 })
});

@jamgold is right - and I would recommend that you follow his advice in using subscriptions, as this will give you full control over the data flow.
The issue isn’t with your collection, it’s just that the data hasn’t reached your client yet, but you’re trying to execute against that data. This is because js doesn’t block - so you need to wait until your subscription has finished loading before calling data you’ve subscribed to.

Check out this article by David Weldon for a better explanation.

I have an application that requires workouts to be loaded and displayed to the user, so in my client code I display a loading indicator (in my case a Material Design Progress Circular component) using the following code;

      // workout results are published by the workoutResultsPublication publication
      // so we need to subscribe to it in order to retrieve them.
      // Last function is an onReady callback to show correct progress indicator state
      this.subscribe('workoutResultsPublication', () => {
        return [
          {},
          this.getReactively('searchText')
        ]
      }, () => {this.isReady = true;});

HTH,
Yon

Yes I didn’t explain it very well :smirk:

3 documents in my collection.

I tried DDP._allSubscriptionsReady() like you said ! it didn’t return anything :disappointed_relieved: !


Template.body.onRendered(function(){
var self = this;
self.autorun(function(){
if( DDP._allSubscriptionsReady() ) {
console.log( Mycollection.find().count() );
}else{
console.log( “******”);
}
})
});


I get ******* in my browser. That means my collection is not ready, no ??

Don’t use autopublish

1 Like

I don’t… but I still have the same pb :frowning:

The solution is :

I have to save the subscription handle and inside the tracker, check if the subscription is ready inside the rendered function:


Tracker.autorun(function©{
if(handle.ready()){
console.log(MyCollection.find().count())
c.stop()
}
});


Best regards,