Session Var as parameter in publish


#1

I’m using a session variable as a parameter to filer my publish list. When I pass the object id as a string the code works and displays the correct objects but using owner: Session.get('id'); does not give any result. Where am I doing it wrong.

Meteor.publish('locations', function() { return Locations.find({ $or: [ { owner: Session.get('id'); } ] }); });


#2

Session is clientside, you need to do something like

Meteor.Subscribe('locations', Session.get('id'));

and on your server

Meteor.publish('locations', function (id) {
...
});

#3

@mordrax Hey Mordrax, Thanks for the reply. But using this also doesn’t seem to work.

Meteor.publish('locations', function(id) {
  return Locations.find({
      $or: [
        { owner: id }
      ]
    });
});

and Meteor.Subscribe('locations', Session.get('id'));

Do I have to pass the find function in my helper as well? I’m fairly new into meteor so please excuse me if my queries seem way too silly to you :smile:

Template.activitySubmit.helpers({
	locations: function() {
		return Locations.find();
	}
});

PS: I have a list with a Apply button for each item where I capture the Object id in the Session variable onclick.


#4

Your code looks right.

Console.log out what the id is on the server, remove the $or to sanity test your query (might be returning no results).
Get mongol to see what is being published to the client.

Actually… find() returns a cursor (unexecuted query), you need to fetch() to get the results (on the client side). Assuming you’re {{> each locations}} in your template. If this fails, everything i’ve said above still applies :slight_smile:


#5

@mordrax I checked it using Mongol and the results were weird

  • When session var was added in subscribe there were no locations
  • When I removed session var from subscribe only the objects which did not have a owner property were displayed.

I console.logged the id via the server and it showed the id. So I guess that the session var is alright.

PS : When I use

Meteor.subscribe('locations', Session.get('id'));

even a hardcoded string id as a parameter for owner does not work?

To give you a general overview of how my application works? There is a list of applications each generated from a Mongodb collection. Each of the item has a book/apply option which takes the user to a form handling page. Now each of the items have different locations available , thus I want to check the ownership of the locations throught a session var which has the ID from the parent object on the previous page. If there is a match, the location is displayed as a option in a drop down list. Hope this gives a general idea of what I’m trying to achieve.


#6

what do you $or your owner id with?