Meteor - Session



Sessions are used for saving data while the users are using the app. This data will be deleted when the user leaves the app.

In this chapter, we will learn how to set a session object, store some data, and return that data. We will use the basic HTML setup.

meteorApp.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <div>
      {{> myTemplate}}
   </div>
</body>
 
<template name = "myTemplate">
</template>

Now, we will store myData locally using Session.set() method. Once the method is set, we can return it using Session.get() method.

meteorApp.js

if (Meteor.isClient) {
  
   var myData = {
      key1: "value1",
      key2: "value2"
   }

   Session.set('mySession', myData);

   var sessionDataToLog = Session.get('mySession');
   console.log(sessionDataToLog);
}

If we check the console, we will see that the stored data is logged.

Meteor Session Log

In the next chapter, we will learn how to auto-update templates using the Session variable.

Advertisements