Hardik Barot
Hardik Barot

Reputation: 775

In sails.js, how to access session variables in method beforeCreate of model?

This is my user model now before create user entity I want to auto give value session variable to createdBy and updatedBY.

       module.exports = {
            autoPK: false,
         attributes: {
                id:{columnName:'ID', type: 'integer', autoIncrement: true,  primaryKey: true},
                employeeId:{columnName:'EMPLOYEE_ID', type: 'string',unique : true},
                firstName:{columnName:'FIRST_NAME', type: 'string'},
                lastName:{columnName:'LAST_NAME', type: 'string'},
                createdBy:{columnName:'CREATED_BY', model: 'user'},
                updatedBy:{columnName:'CREATED_BY', model: 'user'},
        },
        beforeCreate: function(values, cb) {

        values.createdBy=req.session.userId;
        values.updatedBy=req.session.userId;
        return values;
          },
       beforeUpdate: function(values, cb) {

        values.updatedBy=req.session.userId;
        return values;
          },
        }

Upvotes: 2

Views: 1406

Answers (1)

Jim Geurts
Jim Geurts

Reputation: 20419

I was looking for the same thing. Found some relevant resources that may help point you in the right direction. Unfortunately, there isn't great support to have sails automatically set UpdatedBy and CreatedBy properties on models.

Upvotes: 1

Related Questions