Meteor: How to filter data via a session variable

Amir Rahbaran

I have three button to show different type of information

  1. See all (i.e., news AND events)
  2. only news
  3. only events

Status: filtering ONLY news OR events works. But how to see both?

My issue is to write a mongodb query in combination with a session variable. Commented out lines show a failed approach. In my failed approach I tried to put more into the session value (i.e., adding the word type). However, this broke all queries.

My js-file:

Template.newsEvents.helpers({
  newsEventsData: function () {
    // return NewsEvents.find({Session.get('newsEventsView')}, {sort: {createdAt: -1}, limit: 3});
    return NewsEvents.find({type: Session.get('newsEventsView')}, {sort: {createdAt: -1}, limit: 3});
  }
});

Template.newsEvents.events({
    'click #js-seeAll': function (evt, tpl) {
        //Session.set('newsEventsView', '$or: [ { type: "news" }, { type: "event" } ]');

    },
    'click #js-seeNews': function (evt, tpl) {
        //Session.set('newsEventsView', 'type: "news"');
        Session.set('newsEventsView', 'news');
    },
    'click #js-seeEvents': function (evt, tpl) {
        //Session.set('newsEventsView', 'type: "event"');
        Session.set('newsEventsView', 'event');
    }
});

My JSON file:

{
    "_id" : "7sLK32LdoLv4NmtMJ",
    "title" : "3rd News",
    "description" : "Some News",
    "type" : "news",
    "createdAt" : ISODate("2016-01-18T11:23:36.412Z")
}

Any help appreciated.

Brett McLain

Use the $in clause and use an array for your Session variable 'newsEventsView'. I personally use this to filter messages based on their type. The types of the messages are added to an array that is stored as a Session variable and then passed to the $in clause. If the user doesn't want to see a certain type, they click a button which updates the Session variable. Your code would look like this:

Template.newsEvents.events({
    'click #js-seeAll': function (evt, tpl) {
        Session.set('newsEventsView', [ "news", "event" ]);
    }
});

Template.newsEvents.helpers({
    newsEventsData: function () {
        return NewsEvents.find({
            type: {
                $in: Session.get('newsEventsView')
            }, 
            {
                sort: {
                    createdAt: -1
                }, 
                limit: 3
            }
        });
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Meteor #each and session data

From Dev

Meteor and Session/Common Data

From Dev

How to pass encrypted data via browser (HTML5) session variable

From Dev

How do I deal with AWS and Session variable in Meteor?

From Dev

Partially update a session variable in meteor?

From Dev

How to append data in session variable in php?

From Dev

How to delete data based on value in session variable

From Dev

How to call data from session variable?

From Dev

How to append data in session variable in php?

From Dev

How to access the Session in ASP.NET Core via static variable?

From Dev

SqlDataSource - SelectCommand with session variable filter

From Dev

SqlDataSource - SelectCommand with session variable filter

From Dev

Meteor: filter data in publish or on client

From Dev

METEOR: How to manipulate class with session?

From Dev

Meteor + React - Session Variable not behaving reactively

From Dev

Generic session variable key template helper with Meteor

From Dev

Meteor 1.0.2.1 session variable not staying set

From Dev

Session variable not being picked up in meteor helper

From Dev

How to sent data in variable via POST and AFNetworking?

From Dev

How to get session data for a specific page via chrome extension?

From Dev

Filter Data Using Session Id

From Dev

Updating subscribe data during session in Meteor

From Dev

How to create select filter on meteor?

From Dev

How To use Php session variable as Angular Js Data

From Dev

How to filter out JSON data via a particular field?

From Dev

How to filter out specific data from a CSV via Python?

From Dev

Undefined variable session on exporting data from mysql to pdf with ezpdf via php

From Dev

How to unset $_SESSION variable?

From Dev

Store session variable in action filter or static method

Related Related

  1. 1

    Meteor #each and session data

  2. 2

    Meteor and Session/Common Data

  3. 3

    How to pass encrypted data via browser (HTML5) session variable

  4. 4

    How do I deal with AWS and Session variable in Meteor?

  5. 5

    Partially update a session variable in meteor?

  6. 6

    How to append data in session variable in php?

  7. 7

    How to delete data based on value in session variable

  8. 8

    How to call data from session variable?

  9. 9

    How to append data in session variable in php?

  10. 10

    How to access the Session in ASP.NET Core via static variable?

  11. 11

    SqlDataSource - SelectCommand with session variable filter

  12. 12

    SqlDataSource - SelectCommand with session variable filter

  13. 13

    Meteor: filter data in publish or on client

  14. 14

    METEOR: How to manipulate class with session?

  15. 15

    Meteor + React - Session Variable not behaving reactively

  16. 16

    Generic session variable key template helper with Meteor

  17. 17

    Meteor 1.0.2.1 session variable not staying set

  18. 18

    Session variable not being picked up in meteor helper

  19. 19

    How to sent data in variable via POST and AFNetworking?

  20. 20

    How to get session data for a specific page via chrome extension?

  21. 21

    Filter Data Using Session Id

  22. 22

    Updating subscribe data during session in Meteor

  23. 23

    How to create select filter on meteor?

  24. 24

    How To use Php session variable as Angular Js Data

  25. 25

    How to filter out JSON data via a particular field?

  26. 26

    How to filter out specific data from a CSV via Python?

  27. 27

    Undefined variable session on exporting data from mysql to pdf with ezpdf via php

  28. 28

    How to unset $_SESSION variable?

  29. 29

    Store session variable in action filter or static method

HotTag

Archive