0

I'm working in meteor 1.7, and my publish/subscribe is returning only empty arrays. file structure:

-all/
  -shared.js
-client/
  -main.js
-imports/
  -Elsewhere.js
-server/
  -main.js

shared.js:

Chats = new Mongo.Collection('chats')

client/main.js:

Template.main.onCreated(()=>{
  Meteor.subscribe('chats')
});

server/main.js

Meteor.publish('chats', function(){
  console.log(Chats.find().fetch()) //Shows that I have documents in the collection
  return Chats.find();
});

Elsewhere.js

Template.Elsewhere.helpers({
  chats(){
    console.log(Chats.find().fetch()) //[]
    return Chats.find().fetch()
  }
})

Why don't I get what I'm publishing?

-------------------------------------New stuff-----------------------------------

I'm now unsure if it's a load order issue, reactivity issue, pub/sub issue, or a mix of them. I have this snippet

search(collection, where, id, part, callback){
  var result
  if(id){
    console.log(Meteor.users.find().fetch()) //[]
    result = Collections[collection].findOne(id)
  }else{
    result = Collections[collection].find(where ? where : {}, {many:true}).fetch()
  }
  if(result){
    if(callback){
      callback(result)
      return
    }else{
      if(part){
        return result[part]
      }else{
        return result
      }
    }
  }
}

I'm also noticing that the output from this log is happening BEFORE my subscriptions. This file is located in /imports/scripts/tools.js

Spencer Cornwall
  • 289
  • 2
  • 14

2 Answers2

1

Inside an autorun block subscribe to 'chats' and check if handler is ready. Then find and fetch.

this.autorun(() => {
    let handler = Meteor.subscribe('chats');

    if(handler.ready()) {
         console.log(Chats.find().fetch())
    }
  });
1

I realized that I was not using a reactive data source for my helpers, I was setting a Session variable when I called the tool.search function, which only ran once.

Spencer Cornwall
  • 289
  • 2
  • 14