Before we can subscribe to a collection, the collection should exist, and it should be published to the client where we can subscribe to it.

The application below describes how we can subscribe to a collection.

Subscription checklist

The following checklist is adhered to when subscribing to data.

  • Make sure the collection exists.
  • Check that the collection is published to the server.
  • Subscribe to the data inside a useTracker function so that the user interface can be refreshed when the data changes.

Make sure the collection exists

The collection is defined under the api folder, which isinside the imports folder. Remember that all our application codes must be inside the imports folder.

Check that the collection is published to the server

Inside the imports folder, there’s a server folder, and inside of that server folder, there’s a publication.js file with the following content:

import {Meteor} from "meteor/meteor";
import { TasksCollection } from "../api/tasks";

Meteor.publish("tasks.allTask", function(){
    return TasksCollection.find();
});

We import the TasksCollection from its file. Next, we publish our collection, which has the name of tasks.allTask. The publication function returns all the TasksCollection in the system as a cursor.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy