Google News
logo
Meteor.js - Interview Questions
What is Meteor Sessions?
On the client, Session provides a global object that may be used to hold an arbitrary set of key-value pairs. It can be used to save information such as the currently chosen item in a list. When the user exits the app, the data will be removed.
 
Syntax :
Session.set(key, value);
Run the following command in your terminal to add Session to your application :
meteor add session
Creating Meteor Application And Importing Module :
 
Step 1 : Create a React application using the following command.
meteor create foldername
Step 2 : After creating your project folder i.e. foldername, move to it using the following command.
cd foldername
Step 3 : Import Session module from ‘meteor/session’
import { Session } from 'meteor/session'

Step to Run Application : Run the application from the root directory of the project, using the following command.

meteor


Example : This is the basic example that shows how to use the Sessions component.

Main.html :

<head>
    <title>FreeTimeLearn</title>
</head>
 <body>
    <div>
        {{> employee}}
    </div>
</body>
<template name="employee">
    <h1 class="heading">FreeTimeLearn</h1>
    <p>
        Session provides a global object that may
        be used to hold an arbitrary set of key-value pairs.
    </p>
</template>

Main.js :

import { Session } from 'meteor/session';
import './main.html';
var emp = {
    FirstName: "John",
    LastName: "Smith",
    Age: 10,
    Designation: "System Architecture"
}
Session.set('data', emp);
var display = Session.get('data');
console.log(display);

Output : 
Meteor

Source : meteor

Advertisement