Publish from one server Collection to several ones client side

Hello,

I’m trying to fill 2 Client side Collection from one which is server side only, reading around i tried to target specific collection with “Mongo.Collection._publishCursor”, but this just doesn’t seems to work. Is it a way to do this please?

Here is my code, the cursor is well filled in the publish callback but the client collection is not filled :

FoodsPublic = new Mongo.Collection("foods.public");
FoodsSearched = new Mongo.Collection("foods.searched"); 

...

Home = React.createClass({

	mixins: [ReactMeteorData],

...
	getMeteorData () {
		
		
		Meteor.subscribe('foods.public', this.state.requestedFoods, this.state.nameSearched);
		Meteor.subscribe('foods.searched', this.state.requestedFoods, this.state.nameSearch);

	    let query = {};

		return {
			foodsPublic: FoodsPublic.find(query).fetch(),
			foodsSearched: FoodsSearched.find(query).fetch()
		};
	},

...
});

on Server :

Foods = new Mongo.Collection("foods");

setPublishCollectionSimple = function (sub, cursor, collectionName) {
	Mongo.Collection._publishCursor(cursor, sub, collectionName);
	sub.ready();
}

foodByLimitAndSearch = function (limit, nameSearch, collectionName, sub) {
	let query = {};
    let options = {
	    sort: {food_id: 1},
	    limit: Math.min(limit, MAX_FOODS)
	};

	if (nameSearch.length) {
		const searchRegex = new RegExp(nameSearch, 'i');
		query.name =  { $regex: searchRegex }
	}

	console.log("Requesting Food : ", {query:query, options:options})

    //This cursor is filled correctly i can get the data in debugger
	const cursor = Foods.find(query, options);

	setPublishCollectionSimple(sub, cursor, collectionName)
}

Meteor.publish("foods.public", function (limit, nameSearch) {
	foodByLimitAndSearch(limit, nameSearch, 'FoodsPublic', this);
});

Meteor.publish("foods.searched", function (limit, nameSearch) {
	foodByLimitAndSearch(limit, nameSearch, 'FoodsSearched', this);
});
1 Like

Ok it’s a typo, it is working fine when i change the collection name passed to _publishCursor by the mongo collection name (here “foods.public” in place of “FoodsPublic”).

I have the same problem … @blemoine have you solved this problem?