Subscriptions not working


#1

server\main.js

import { Meteor } from 'meteor/meteor';
Items = new Mongo.Collection('test');

Meteor.startup(() => {
let data = [
  {a:1,b:2},
  {a:21,b:42},
  {a:31,b:42}
];

data.forEach((item) => Items.insert(item));
Meteor.publish('testsub', function(limit) 
  {
     console.log(`publishing items count = ${Items.find().count()}`);
     return Items.find({limit: limit});
  });
});

client\main.js

import { Meteor } from 'meteor/meteor';
Items = new Mongo.Collection('test');

Meteor.subscribe('testsub');
console.log(`Subscribed count = ${Items.find().count()}`);

Nothing shows up on the client. I can run the client subscribe inside a Tracker.autorun and it still doesn’t. What am I doing wrong?


#2

For one, you should declare your Mongo Collection only once, in a folder that is reachable by both the client and server, e.g. [rootOfYourApp]/lib/collections.js.

The other question I would have is where your limit variable comes from that you provide to your publishing function. It doesn’t seem like you’re defining it anywhere?


#3

Your query is (probably) incorrect.

I think you want something along the lines of:

Meteor.publish('my-publication', function ({ limit }) {
    return Items.find({}, { limit });
});

Obviously replace {} with the actual query you want, or leave as-is to return all documents in the collection.


#4

I made a mistake copying the code in here, I do have the limit param in the query function. Also including the same file for the Items doesnt change things - I actually have an imports folder with all the code.

Edited my original post.


#5

But the data you’re inserting doesn’t have a ‘limit’ field so it’s always going to return 0 results. Or is the actual code different?