How to get session parameters in afterCreate of sail model
I want to get the current user login in the models callback afterCreate
. In the controller, the currently logged in user is found from the request header - req.user
. But in the model lifecycle callback, the session header is not passed. Is there a way to find the current user from the model?
I need something like this:
afterCreate: function(record,next){
var currentUser=this.req.user.id; //want to get the request header here with logged in user
AnotherModel.create({something:record.something, createdBy:currentUser}).exec(function(err, anotherRecord){
next()
});
}
source to share
The req object is not available in the model (for reasons I won't go into).
But your problem is common. What a lot of people have done is create a policy that will automatically set this parameter for the parameters passed to the model.
Here are some related questions and answers
sails.js Use session parameter in model
Is it possible to access a session variable directly in a model in SailsJS .
https://github.com/balderdashy/waterline/issues/556
source to share