1

Assume that I have a Collection called Tasks which has few tasks in it.
I call a method to return a task array to the user but for some reason it doesn't return anything.

Here is a code for example:

if (Meteor.isClient) {
// This code only runs on the client
    Template.body.helpers({
       tasks: function () {
            // Show newest tasks first
            Meteor.call("getTasks", function(error, result) {
                return result; // Doesn't do anything..
            });
        }
    });
}

Meteor.methods({
    getTasks: function() {
        return Tasks.find({}, {sort: {createdAt: -1}});
    }
});

Any ideas why when I call the method it doesn't return anything?

julian
  • 4,634
  • 10
  • 42
  • 59
  • possible duplicate of [How to use Meteor methods inside of a template helper](http://stackoverflow.com/questions/22147813/how-to-use-meteor-methods-inside-of-a-template-helper) – David Weldon Feb 06 '15 at 04:40

1 Answers1

4

Tasks.find() returns a cursor, which makes no sense to transmit to the client via DDP.

You probably mean to return Tasks.find().fetch(), but that defeats the purpose of Meteor's very nice data synchronization mechanism.

Have you read Understanding Meteor's publish/subscribe?

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • But typing `return Tasks.find({}, {sort: {createdAt: -1}});` instead of the method call works. – julian Feb 06 '15 at 03:29
  • It works in the console because the autopublish package is added by default, which sets up a publication on server and a subscription on the client automatically. Seriously, read up on that "Understanding" answer :) – Dan Dascalescu Feb 06 '15 at 03:35
  • Thanks! I also read this and it really explained the system well! (and it's written by Sacha Greif too): https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/ – julian Feb 07 '15 at 04:22