Ian Jones
Ian Jones

Reputation: 1509

Meteor returns Cursor object, but not able to .count()

I don't understand why this cursor returns a zero count:

Template.productList.helpers({
    'products': function() {
        return Products.find({}, {transform: function(doc) {
            console.log(ProductsLocations.find({product_id: doc._id}).count());
            return doc;
        }});
    }
});

When I console.log() it without .count() I get an object I can traverse in Chrome Dev Tools.

The values exist in the database and doc._id is a valid ID

Upvotes: 0

Views: 77

Answers (1)

Dan Dascalescu
Dan Dascalescu

Reputation: 151926

Do you use autosubscribe?

You probably need to make sure the subscription is ready. See Meteor: How can I tell when the database is ready? and Displaying loader while meteor collection loads.

The reason you do see the object in the console is that by the time you make that call, the subscription is ready. But in your helper code you might have assumed that the subscription data has arrived when in fact it has not.

A useful debugging tool is Chrome's device mode ("phone" icon near the search icon in DevTools), which lets you simulate slow networks (e.g. GPRS, with 500ms delay for every request).

Learn more about Meteor's publish/subscribe and check out https://www.discovermeteor.com/blog/template-level-subscriptions/ for the latest pattern.

Upvotes: 1

Related Questions