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
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
I want to manipulate this cursor
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()
@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;});