I have made a recipe app. I am listing all the recipes from the database. I want to add a feature that filters the recipes, to only show the user's recipes.

I have published the recipe collection and subscribed to it in a different file. It loads all the recipes just fine. But when I click to only show the user recipes, no recipes show and I get an error message saying "You are on client so you must either provide a callback to get the data or subscribe first"

I am confused because I thought I was subscribed to the collection since it populates all the recipes. Yet when I try to filter the data I am told I need to subscribe.

My publication looks like this and this is the path.

imports/api/recipes.js

if (Meteor.isServer) {
    // This code only runs on the server
  Meteor.publish('recipes', function recipesPublication() {
    return Recipes.find({
    });
  });
}

I have a body.js file that imports that file and subscribes to the data. It displays all the recipes so I assume it is subscribing to the Recipe collection. The if statement handles which recipes should display. If it is false it shows all recipes and that works fine. If it is true it will only show the recipes that belong to the user. This is where I get the error message. The odd part that confuses me is the commented out Mongo query command will filter the data. So how come I am told I need to subscribe to the data when using Grapher?

Template.body.onCreated(function bodyOnCreated() {
    this.state = new ReactiveDict();
    Meteor.subscribe('recipes');
    Meteor.subscribe('ingredients');
  });

Template.body.helpers({
  recipes() {
      const instance = Template.instance();
      if (instance.state.get('showMyRecipes')) {
        // return Recipes.find({ owner: Meteor.userId() }, { sort: { createdAt: -1 } });
        const query = Recipes.createQuery({
          $filters: {
               owner: Meteor.userId(),
           },
           name: 1,
           instructions: 1,
        });
        return query.fetch();
      }
      return Recipes.find({}, { sort: { createdAt: -1 } });
    },
    ingredients() {
          return Ingredients.find({});
        },
});
0

There are 0 best solutions below