Reputation: 136
It is simple to get access to session
property from the action of controller.
var SomeController = {
someAction: function(req, res) {
// no we have access to session object
if (!req.session.hasOwnProperty('flash')) {
req.session.flash = [];
}
}
}
But I need to get access to session
object from service.
Example file app/services/my_servise.js
:
module.exports = {
some_method: function() {
// here at i need to get access to session object?
// is it possible?
}
}
Upvotes: 1
Views: 980
Reputation: 24948
See this answer for an extended discussion of why you can't access session params in model class methods; the exact same answer holds true for services. The upshot is that you must pass the request object as an argument to your service method if you want access to the session in the service code.
Upvotes: 1
Reputation: 568
The solution very much depends on when my_service.js is called and for what purposes it is used. Here is the easiest one:
var my_service, SomeController;
my_service = require('./services/my_service.js'); // Assuming this module is in the /app directory.
SomeController = {
'someAction': function(req, res) {
if (!req.session.hasOwnProperty('flash')) {
req.session.flash = [];
}
my_service.some_method(req);
}
}
/app/services/my_service.js:
module.exports = {
'some_method': function (req) {
// use req.session
}
};
If that's not what you're looking for, please explain my_service.js its usage a little more.
Upvotes: 0