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

Meteor.publish('recentPolls', function() {
    return Polls.find({}, {sort: {date: -1}, limit: 10}).fetch();
});

So this is in my /server/server.js file from the documentation it says the fetch() method returns matched documents in an array. However, using the subscribe function in the client like so

Template.recentPolls.polls = function() {
    console.log(Meteor.subscribe('recentPolls'));
    return Meteor.subscribe('recentPolls');
}

For some odd reason this is returning the following object (not an array) but an object

Object {stop: function, ready: function}

And this is the error I get in the console.

Exception from sub 5NozeQwianv2DL2eo Error: Publish function returned an array of non-Cursors
user1952811
  • 2,398
  • 6
  • 29
  • 49

1 Answers1

4

fetch returns an array of objects, which isn't a legal value to return from a publish function.

Publish functions can only return a cursor, an array of cursors, or a falsy value. To fix your error, just remove the fetch:

return Polls.find({}, {sort: {date: -1}, limit: 10});

On the client you do not want to subscribe inside of your templates. You want to either subscribe once (usually in a file called client/subscriptions.js) or inside of your route (see the iron-router documentation).

Ignore whatever the subscribe returns. Calling subscribe just allows the server to sync data to the client. The result of the call is not the data itself.

To access your data from your template, just use another find like:

Template.recentPolls.polls = function() {
  Polls.find({}, {sort: {date: -1}});
}
David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • That just returns the cursor correct? How would I go about accessing the data in the client? – user1952811 Mar 01 '14 at 20:54
  • This is the error I get in the console: `Error: {{#each}} only accepts arrays, cursors, or falsey values. You passed: [object Object]` – user1952811 Mar 01 '14 at 21:00
  • The issue is that it's still returning the same object. `console.log(Meteor.subscribe('recentPolls'));` returns the same object `Object {stop: function, ready: function}` – user1952811 Mar 01 '14 at 21:06
  • Yes, `find` returns a cursor. I updated the answer - see if any of that helps. Please make sure `Polls` is defined on both the client and the server. – David Weldon Mar 01 '14 at 21:30
  • This solved my problem, which was the same error. I was subscribing in a `RouteController` and returning that data to the template. It worked when I moved the subscribe to a global call and changed the controller to a simple `find()`. – Tim Fletcher Dec 26 '14 at 12:15