mrawesome
mrawesome

Reputation: 33

How to properly subscribe to collection on Meteor client side?



First of all, I'm not a newbie to Meteor, but after the latest Meteor updates I have to re-study the framework, and now I'm having trouble using Meteor subscription on the client side.

To be specific, I have subscribed a collection on the client side, however when I try to refer to it the browser console reported the error:

Exception in template helper: ReferenceError: Chatbox is not defined

Here's the structure of my code:


imports/api/chatbox/chatboxes.js

// define the collection
export const Chatbox = new Mongo.Collection("chatbox");      


imports/api/chatbox/server/publication.js - to be imported in server/main.js

import { Meteor } from "meteor/meteor";
import { Chatbox } from "../chatboxes";

Meteor.publish("chatbox", function(parameter) {
    return Chatbox.find(parameter.find, parameter.options);
});


imports/ui/chatbox/chatbox.js - page template to be rendered as content upon routing

import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';

import './chatbox.html';
import './chatbox.css';

Template.chatbox.onCreated(function bodyOnCreated() {
    this.state = new ReactiveDict();
    // create subscription query
    var parameters = {
        find: {
            // query selectors
            permission: "1001",
        },
        options: {
            // query options
        }
    };
    Meteor.subscribe("chatbox", parameters);
});

Template.chatbox.helpers({
  canAddMore() {
      // Chatbox collection direct access from client
      return Chatbox.find().count() < 3;     
  },
});



I'd appreciate if you can help me with this issue. Thanks all for taking your time reading my question!

Regards

Upvotes: 1

Views: 760

Answers (1)

Jeremy S.
Jeremy S.

Reputation: 4659

You need to import Chatbox in imports/ui/chatbox/chatbox.js:

import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { Chatbox } from "../chatboxes"; // correct this path

It's undefined right now because it hasn't been imported.

Upvotes: 1

Related Questions