Collection not working on client

Hi,

Any Idea why this is not working on the client?
On server I get the correct count but on client I get ‘0’

this is in /lib/collections.js:

if (Meteor.isServer) {
   let database1 = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/HerbsDB");
   export const Herbs = new Mongo.Collection("HerbsTable", { _driver: database1 });
   let a = Herbs.find().count()
   console.log(a);
   Meteor.publish('Herbs', function() {
     return Herbs.find();   
   });                      
 }

 if (Meteor.isClient) { 
  export const Herbs = new Mongo.Collection("HerbsTable");
  Meteor.subscribe('Herbs');
  let a = Herbs.find().count()
  console.log(a);
}

on dev console I get:
Herbs.find()
VM7535:1 Uncaught ReferenceError: Herbs is not defined(…)

HerbsDB is a database and you don’t query them directly with ‘find()’ as what you did…and that’s the reason its undefined

Instead, do a collection query like Herbs.find()

My mistake ( I edited the original post) . I do use Herbs.find() and I get “Herbs is not defined(…)”

can you change your code to this.

if (Meteor.isClient) { Herbs = new Mongo.Collection("HerbsTable"); Meteor.subscribe('Herbs'); let a = Herbs.find().count() console.log(a); }

taking out the export const… i am not sure whether you are using imports because that’s usually happened.

I get the same “Herbs not defined…”

is that error coming from your console or web console? the error should show you what file did it come from

web console from collections.js

please comment this part and see if the error still there

if (Meteor.isClient) { 
  // export const Herbs = new Mongo.Collection("HerbsTable");
  // Meteor.subscribe('Herbs');
  // let a = Herbs.find().count()
  // console.log(a);
}

and then this one

if (Meteor.isClient) { 
   export const Herbs = new Mongo.Collection("HerbsTable");
   Meteor.subscribe('Herbs');
  // let a = Herbs.find().count()
  // console.log(a);
}

I think there’s a bit of a mismatch between expected “classic” Meteor behaviour and new ES6 Meteor behaviour.

let and const are block scoped - that means that the Herbs defined in your isServer is not the same as the Herbs in your isClient block.

I’m not a huge fan of Meteor.isClient and Meteor.isServer. However, you could try this (untested):

export let Herbs;
if (Meteor.isServer) {
  const database1 = new MongoInternals.RemoteCollectionDriver('mongodb://127.0.0.1:3001/HerbsDB');
  Herbs = new Mongo.Collection('HerbsTable', { _driver: database1 });
  const a = Herbs.find().count();
  console.log(a);
  Meteor.publish('Herbs', function () {
    return Herbs.find();
  });
}

if (Meteor.isClient) {
  Herbs = new Mongo.Collection('HerbsTable');
  Meteor.subscribe('Herbs');
  const a = Herbs.find().count();
  console.log(a);
}

[ Edit : to alter database1 scoping and for export ]
[ Edit : not sure about this any more. Apparently there are problems with mutable exports. ]

I should also point out that you aren’t waiting for the data to arrive in your subscribed collection, so you may want to check for the subscription ready before reporting.

Your subscribtion is not ready yet when you call subscribe: your data hasn’t arrived yet!

Change your client code to this:

if (Meteor.isClient) {
  export const Herbs = new Mongo.Collection("HerbsTable");
  const sub = Meteor.subscribe('Herbs');
  Tracker.autorun(() => {
    if(sub.ready()) {
      let a = Herbs.find().count()
      console.log(a);
    } else {
      console.log('Loading...');
    }
  });
}

@akryum , @robfallows - tnx! the wait for the subscription to be ready is working and it shows the count.

However, when trying to query via the web console “Herbs.find()” - I still get
Uncaught ReferenceError: Herbs is not defined(…)

Try using meteor globals:

if (Meteor.isClient) {
  Herbs = new Mongo.Collection("HerbsTable");
  const sub = Meteor.subscribe('Herbs');
  Tracker.autorun(() => {
    if(sub.ready()) {
      let a = Herbs.find().count()
      console.log(a);
    } else {
      console.log('Loading...');
    }
  });
}

I tried that, but result is same

const Herbs = window.Herbs = new Mongo.Collection("HerbsTable");

I have same/similar issue. Bookmarking this for possible future solution.

EDIT: the trick with global name does seem to work…