user1952811
user1952811

Reputation: 2458

Meteor Collection (subscribe and publish) unexpected returns

So this is what I've got in my router.js file in root of my app folder

this.route('pollyShow', {
    path: '/polly/:_id',
    template: 'polly_show',
    notFoundTemplate: 'notFound',
    waitOn : function () { 
        return Meteor.subscribe('polls_all');
    },
    before: function () {
        Meteor.subscribe('polls_all');
        var Polls = new Meteor.Collection('polls_all');
        console.log(Polls);
        var id = this.params._id;
        var poll = Polls.findOne({_id: id});
        console.log(poll);
    },
});

and i've got this in my server/server.js file

Meteor.publish('polls_all', function () { 
    return Polls.find({}); 
});

I get the following errors in my console.log

console.log(Polls) returns the following Meteor.Collection {_makeNewID: function, _transform: null, _connection: Connection, _collection: LocalCollection, _name: "polls_all"…}

Which is obviously not what I am expecting.

Then console.log(poll) returns undefined as expected since console.log(Polls) is returning something completely unexpetected.

Finally I get the following error.

Exception from Deps recompute: Error: There is already a collection named 'polls_all'
    at new Meteor.Collection (http://localhost:3000/packages/mongo-livedata.js?32cb8e7b8b1a4ecda94a731b3a18e434e3067a5f:263:13)
    at route.before (http://localhost:3000/router.js?f11d1e915689f0ca34e03814eb5acc76c7d2d798:15:16)
    at IronRouteController.runHooks (http://localhost:3000/packages/iron-router.js?7a9e077ee92fd60193e5d33532c9c2406c28cb5b:649:12)
    at Utils.extend.run (http://localhost:3000/packages/iron-router.js?7a9e077ee92fd60193e5d33532c9c2406c28cb5b:2024:10)
    at null._func (http://localhost:3000/packages/iron-router.js?7a9e077ee92fd60193e5d33532c9c2406c28cb5b:1484:22)
    at _.extend._compute (http://localhost:3000/packages/deps.js?eba25ec453bb70e5ae5c2c54192d21e0e4d82780:183:12)
    at _.extend._recompute (http://localhost:3000/packages/deps.js?eba25ec453bb70e5ae5c2c54192d21e0e4d82780:196:14)
    at _.extend.flush (http://localhost:3000/packages/deps.js?eba25ec453bb70e5ae5c2c54192d21e0e4d82780:288:14) 

I am extremely new to the publish and subscribe methods since I've been using the autopublish package. I am trying to switch over and I am having a lot of difficulties.

Upvotes: 0

Views: 1727

Answers (1)

David Weldon
David Weldon

Reputation: 64312

You need to define your collections once. In the code above, Polls is defined every time the pollyShow route is run. Given that this is a collection which will be shared on both the client and the server, you should add the definition inside of a shared directory under your application root like:

lib/collections/polls.js

Polls = new Meteor.Collection('polls');

I'm a little confused by your question though - I'm assuming the collection is actually called polls and the publish identifier is polls_all (these are very different things).

You also don't need to add Meteor.subscribe('polls_all'); in your before hook since you are already waiting on it.

Upvotes: 2

Related Questions