Moving "Meteor.publish" to server folder in Blaze Tutorial?


#1

Hi there,

I’m not too sure if this matters, but I was going thru the Blaze tutorial. Everything worked fine. I did notice there was this line in the “imports/api/tasks.js” file.

export const Tasks = new Mongo.Collection('tasks');

if (Meteor.isServer) {
    // This code only runs on the server
    // Only publish tasks that are public or belong to the current user
    Meteor.publish('tasks', function tasksPublication() {
        return Tasks.find({
            $or: [
                { private: { $ne: true } },
                { owner: this.userId },
            ],
        });
    });
}

Rather than using the “isServer”, could I move this into the server folder? I tried to created a publications.js file in the server folder and moved that block into it. I thought I could just add a

Tasks = new Mongo.Collection('tasks');

on top and it should work properly, but I get a “Error: A method named ‘/tasks/insert’ is already defined”. If I remove that line since it has possibly been defined by the tasks.js file, the app loads but a refresh causes a “Exception from sub tasks id qb3exBpzJzLXP8Kii ReferenceError: Tasks is not defined”

I must be missing something. Hoping someone could shed some light as to what the problem is. If you need a reference to the full file, heres the link to the finished file. Here

Thanks!
T


#2

Don’t redefine the collection, just import it from the module

import { Tasks } from '/imports/api/tasks'

#3

I knew I missed something simple. Appreciate the help!


#4

NP I’m just started Meteor and JavaScript programming a few weeks ago anyway. :slight_smile: