0
votes

My collection is created in collections.js

portfolioCategories = new Mongo.Collection('portfoliocategories');

It is then subscribed to in subscriptions.js

Meteor.subscribe('portfoliocategories');

And published in publications.js

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

If I query Mongo from the server with db.portfoliocategories.find() I'll get

{ "_id" : "W9AeauCpMgPw2j5hf", "title" : "Commercial Design", "slug" : "commercial-design", "image" : "https://url/9c3ba46d-c09a-4a7d-ac40-5752fc24ad2c.jpg" }

From the client and ironrouter both, however, find returns nothing. For example if I type into the console:

portfolioCategories.find({'_id':'W9AeauCpMgPw2j5hf'});

I will get a LocalCollection.Cursor with undefined key values:

_selectorId: undefined

_transform: null

collection: LocalCollection

fields: undefined

limit: undefined

The same thing happens in iron-router if I try to return that as data. Yet, if I use findOne I'll get the document.

portfolioCategories.findOne({'_id':'W9AeauCpMgPw2j5hf'})

Object { "_id" : "W9AeauCpMgPw2j5hf", "title" : "Commercial Design", "slug" : "commercial-design", "image" : "https://url/9c3ba46d-c09a-4a7d-ac40-5752fc24ad2c.jpg" }

My issue is that I need to return all items with the the same title. Because of this, findOne() is not a proper solution.

What am I missing?

1
find returns a cursor, on which you can call fetch to get an array of documents. A complete answer can be given with more details on how you want to use these documents. - David Weldon
So is proper syntax something more like portfolioCategories.find().fetch() ? - thatgibbyguy
Another curiosity, however, is that what I'm trying to do works on other templates. For example featuredItems: function () { return projectsPortfolio.find({'featured':true}); } works just fine and as it should. - thatgibbyguy
(a) yes that's the syntax if you want an array of documents, (b) yes, templates accept both cursors and arrays as inputs. - David Weldon
Thank you for working through this with me, my rubber duck was no help. I'll provide a follow answer as the real issue is a discrepancy in my understanding of iron-router's data object and template helpers. - thatgibbyguy

1 Answers

1
votes

The confusion came because of the differences between iron-router's data object. What I was trying to do from iron-router was

waitOn:function(){

    return [Meteor.subscribe('projectsportfolio'),Meteor.subscribe('portfoliocategories')];

}
data:function(){
    currentSlug = this.params.category;
    currentCategory = projectsPortfolio.find({slug:currentSlug});
    if(typeof currentCategory != 'undefined'){
        return currentCategory
    }
}

But this only returns a cursor as @David Weldon explained. The solution is:

data:function(){
    currentSlug = this.params.category;
    currentCategory = projectsPortfolio.find({slug:currentSlug}).fetch();
    if(typeof currentCategory != 'undefined'){
        return currentCategory
    }
}

currentCategory will now be an array of objects that I can now loop through in my template as

{{#each currentCategory}}
    {{keyvalue}}
{{/each}}