Can't Return Collection from Subscription Meteor JS (I'm a noob)


#1

What am I doing wrong here? I think I might be a little confused about the file structure, and must have a simple variable mixed up somewhere in one of the files. I’m just getting an empty array from my subscribe.

This is what it looks like:

body.js

import Celebrities from "../api/celebrities.js";

/* Import the other templates*/
import "./celebrity.js";

…/api/celebrities.js

import {Mongo} from "meteor/mongo";
import "./methods";

const Celebrities = new Mongo.Collection('celebrities');
export default Celebrities;

if( Meteor.isServer ){
  Meteor.publish('allCelebrities',function(){
    return Celebrities.find({});
  });
}

body.js

/* When the body is created*/
Template.body.onCreated(function(){
  // Subscribe to the item
  Meteor.subscribe('allCelebrities');

body.js

/* The helpers for this body*/
Template.body.helpers({
  celebrities: function(){
    return Celebrities.find();
  },

Does this code look correct? If I run a find() from the console I have no issues.


#2

I figured this out. It was related to the server/main.js file not including the requisite api/*.js file. I got it now.


#3

I recommend you use template based subscription,

Template.body.onCreated(function(){
  // Subscribe to the item
  this.subscribe('allCelebrities');
})

which will provide you with nice helpers

<template name="body">
 {{#if Template.subscriptionsReady}}
  <!-- do stuff with the data -->
 {{else}}
  Loading ...
 {{/if}}
</template>