Hello,
I’m trying to publish collections with a filter, by default the filter is none, so it should return everything the user has.
Works
Trips.find({filter}, {});
Works
Trips.find({userId: this.userId}, {});
Does not work
Trips.find({filter, userId: this.userId}, {});
This is what I see server-sided
Meteor.publish('trips', function(filter, limit, skip){
if (!Roles.userIsInRole(this.userId, 'admin')) {
return Trips.find({filter, userId: this.userId}, {limit:limit, skip:skip, sort: {createdAt: -1} }); //if I comment out 'filter' I start to see everything as the user
} else {
return Trips.find(filter, {limit:limit, skip:skip, sort: {createdAt: -1} }); //Works fine here, filter does work!
}
});
Client-sided to refresh the subscription
Template.triphtml.onCreated(function(){
var instance = this;
Session.set('limit',defaultLimit);
instance.autorun(function(){
instance.subscribe('overtime', Session.get('overFilter'), Session.get('limit')||defaultLimit, Session.get('overSkip')||0);
instance.subscribe('trips', Session.get('tripFilter'), Session.get('limit')||defaultLimit, Session.get('tripSkip')||0);
});
Session.setDefault('tripFilter', {});
Session.setDefault('limit', 20);
Session.setDefault('tripSkip', 0);
var defaultSkipStep = 20;
var defaultLimit = 20; //
Meteor.subscribe('overtime', Session.get('overFilter'), Session.get('limit')|| defaultLimit, Session.get('overSkip')|| 0);
Meteor.subscribe('trips', Session.get('tripFilter'), Session.get('limit')|| defaultLimit, Session.get('tripSkip')|| 0);
How I change the filter for example
const carExpr = carSelected != "anycar"? {"car": {$eq: carSelected}} : {};
const corpExpr = companySelected != "anycorp"? {$or: [ { "a": { $eq: companySelected } }, { "b": { $eq: companySelected } }]} : {};
const moExpr = monthNumber != "0"? {"month": { $eq: monthNumber}} : {};
const uFilterExpr = {"userId" : {$eq: Meteor.userId()}};
const yearExpr = {"year": {$eq: yearNum}};
const allExpr = Object.assign({}, carExpr, corpExpr, moExpr, uFilterExpr, yearExpr);
Session.set('tripFilter', allExpr);
Filter option does work if user has role admin tho.