Meteor MongoDB find / fetch issues

走远了吗. 提交于 2019-12-22 18:24:52

问题


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

回答1:


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}});
}


来源:https://stackoverflow.com/questions/22119873/meteor-mongodb-find-fetch-issues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!