Julien Le Coupanec
Julien Le Coupanec

Reputation: 7996

Meteor: Subscription doesn't work

I'm trying to get a document from the server and display it on the client but the subscription always return a collection with no document.

// server/publications.js
Meteor.publish('myPages', function() {
    return Pages.findOne({userId: this.userId});
});

// collection/pages.js
MyPages = new Meteor.Collection('myPages');

// client/main.js
Meteor.subscribe('myPages');

// client/view.js
Template.myView.helpers({
    myPages: function(e, t) {
        console.debug(MyPages.find({}));
        return MyPages.find({});
    }
});

Upvotes: 0

Views: 431

Answers (2)

Hubert OG
Hubert OG

Reputation: 19544

You cannot move a document between collections via a subscription. If you subscribe to get a document that's in Pages collection, defined as new Meteor.Collection("pages"), then no matter how your pubsub channels look like, on the client the document will be found in the collection defined as new Meteor.Collection("pages"). So remove all traces of MyPages and use Pages on the client as well. You'll find the document there.

Upvotes: 1

Christian Fritz
Christian Fritz

Reputation: 21384

I don't think you can use findOne to publish collections: it doesn't return a cursor but an actual object.

Does this not work?

Meteor.publish('myPages', function() {
    return Pages.find({userId: this.userId});
});

or, if necessary:

Meteor.publish('myPages', function() {
    return Pages.find({userId: this.userId}, {limit: 1});
});

Upvotes: 1

Related Questions