Meteor subscribe and publish not working

Please help.
I have a collection called ChatList which i have triple checked (before removing the autopublish package) that it has 5 documents in it. The autopublish package is now removed and with the following code, all it returns is an empty collection in the chrome console. That is, when ChatList.find().fetch() is entered, all that it shows is [ ].
What am i doing wrong? its driving me crazy.

mychatapp > server > server.js

if(Meteor.isServer){

Meteor.publish(‘chatsLists’,function(){
var currentUser = this.userId;
return ChatList.find({createdBy: currentUser});
});

}

mychatapp > client > client.js

if(Meteor.isClient){

Meteor.subscribe(‘chatLists’);
}

mychatapp > mychatapp.js

ChatList = new Mongo.Collection(‘chats’);

Are you shure this.userId is defined (user is logged) and colllection has documents for current userid? Note that autopublish publishes all the collections without any filters.

1 Like

There is a typo. You are publishing ‘chatsLists’ but subscribing ‘chatLists’ (missing s).

1 Like

I thought you were going to be my savior with the ‘s’ but it didn’t fix it. :frowning:
And i just removed the var currentUser line to have it be a non-factor.

Still not working though. any additional suggestions? logged in or out, still getting the empty array.

If you change this to:

return ChatList.find();

Does the whole collection get published to the client?


also side note, you don’t need the conditionals if(Meteor.isClient){ and if(Meteor.isServer){ when using files in the dirs: /client and /server

1 Like

Still not working. Also removed the isServer and isClient. Here is what I have now

mychatapp > mychatapp.js
ChatList = new Mongo.Collection(‘chats’);

server > server.js

Meteor.publish(“chatLists”,function(){
return ChatList.find();
});

client > client.js

Meteor.subscribe(“chatLists”);

ChatList.find().fetch(); still returns
and
ChatList.find(); return something but doesnt look like what i want (see attached)

This clearly tells that there are no documents.

1 Like

To verify, open a mongo db console:

meteor mongo

Now type

db.chats.find();

meteor:PRIMARY> db.chats.find();

You should get all documents (if any)

Exactly what @rajivguliani has said. Are you sure there are any documents in the collection?

The other thing i was going to suggest was maybe you haven’t defined the collection properly.
However from that console screenshot it looks fine because its got all the right attributes.

You should install the free version of Meteor Toys.
With Mongol you can debug pubs and subs quiet effectively.

You may try next (actually this is how autopublish works)

Meteor.publish(null, () => ChatList.find());

Since you are using this.userId to filter out the appropriate chats from your collection, I assume you use the accounts system.

Have you tried wrapping the subscribe into Tracker.autorun so it gets refreshed when the user actually logs in? You could also add a console.log(this.userId) to your publish function to see what your server is actually querying the collection for.

Tracker.autorun(function(){
 var userId = Meteor.userId();
 Meteor.subscribe('chatList');
});

Thank you everyone for all the help. This community is great.
I came home from work yesterday and it randomly started working properly without changing anything. not sure what happened

thanks!!

And now I am suffering from the same problem. When will my random moment come?

Share your code with us then we can try to help you. :slight_smile: