meteor Publishing Data Creating and responding to an error on a publication.


On the server, you can create a publication like this. this.userId is the id of the user who is currently logged in. If no user is logged in, you might want to throw an error and respond to it.

import Secrets from '/imports/collections/Secrets';

Meteor.publish('protected_data', function () {
  if (!this.userId) {
    this.error(new Meteor.Error(403, "Not Logged In."));
  } else {
    return Secrets.find();

On the client, you can respond with the following.

Meteor.subscribe('protected_data', {
  onError(err) {
    if (err.error === 403) {
      alert("Looks like you're not logged in");

File /imports/collections/Secrets creates reference to the secrets collection as below:

const Secrets = new Mongo.Collection('secrets');