Search code examples
meteormeteor-blaze

Displaying cursor data within a blaze template does not work with Meteor.call()


ı have a very simple template as follows;

<template name="editingUsers">
    <div class="container">
        <div class="jumbotron">
            <ul class="custom-list-stye">
                {{#each lastEditors}}
                    <li><span><strong>{{docUser.name}}</strong></span> </li>
                {{/each}}
            </ul>
        </div>
    </div>
</template>

I am filling the list in the HTML with an helper;

Template.editingUsers.helpers({
    lastEditors:  function () {
        return Meteor.call('getLastEditors');
    }
});

The 'getLastEditors' method returns data by a MongoDB query;

getLastEditors: function () {
        if(Meteor.user()) {
            const lastEditors = Documents.find(
                {docUser: {$exists: true }},
                {
                    limit:5,
                    sort: { lastEdit: -1 },
                });
            return lastEditors;
        }
    }

With this code, the data that should be displayed as a list does not appear. however if I make the Mongo DB query directly from the helper everything becomes OK. The following is the working helper code. (The autopublish package is not removed)

Template.editingUsers.helpers({
    lastEditors:  function () {
        return Documents.find(
            {docUser: {$exists: true }},
            {
                limit:5,
                sort: { lastEdit: -1 },
            });
    }
});

As you know, ı cannot continue with autopublish package and I have to implement Meteor.methods(). I don't understand why returning cursor data from Meteor.method is not shown within the template. Can I have your comments please?


Solution

  • The correct way to replace the autopublish package is to implement the Publish and Subscribe pattern (which is what autopublish does under the hood), not to use Meteor methods.

    Your last code example (where you search your Documents collection directly in the Client template helper) is perfectly fine. You just need to setup a Publish on your server with at least those documents (you can publish even more if necessary), and Subscribe to it, typically when your template is created.

    As a short term workaround, you could use an intermediate ReactiveVar to display your cursor in your template helper.

    See feed helper from a callback

    Furthermore, on your Client you have to use Meteor.call with a callback. It does not return anything.

    See http://docs.meteor.com/api/methods.html#Meteor-call