Subscription with filter opt gives 0 values in return


#1

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.


#2

Apparently the issue is with the syntax.
This is how filter may look like at any given time

{month: {…}, userId: {…}, year: {…}}
month
:
{$eq: "5"}
userId
:
{$eq: "5QSwS8XLAzCbEJdrj"}
year
:
{$eq: "2017"}
__proto__
:
Object

Works

Trips.find({filter}, {});

Works

Trips.find({userId: this.userId}, {});

Does not work

Trips.find({filter, userId: this.userId}, {});

#3

Sooo … is filter an object? It seems to be.
So if we assume the filter is {key: value} then Trips.find({filter, userId: this.userId}, {}); would be Trips.find({ {key: value}, userId: this.userId}, {}); This will not work. You could just change the filter in the publication and add the userId. (filter.userId = this.userId)


#4

I believe filter is an array of objects
like
{month: {…}, userId: {…}, year: {…}}


#5

Okay, so after messing around this seems to work perfectly.

return Trips.find(  (filter , {userId: this.userId}) , {limit:limit, skip:skip, sort: {createdAt: -1} });

Thanks!