Is it possible to access a session variable directly in a model in SailsJS

I have a situation where I would like to use a Sails project to create a model. However, I need to access the session variable that creates:

Url: / api / myModel / create [post]

Model: module.exports = {

    adapter: 'mongo',
    schema: true,
    attributes: {
        user: {
            model:'user',
            required:true,
            index:true
        },
        item: {
            model:'item',
            required:true,
            index:true
        },
        quantity: {
            required:true,
            type: 'integer',
            defaultsTo: 1,
            min: 0
        },
        size: {
            required:true,
            type:'string'
        },
        container: {
            required:true,
            type:'string'
        },
        dateManuf: {
            required:true,
            date:true
        }
    },
    beforeValidation:function(values, next) {
        /* I want to automatically set the logged in 
           USERID here */

        next();
    }
};

      

I want to automatically set the value of the logged in user userid userid to a field. Do I have to create my own custom route / controller action for this to access the "req" field correctly?

0


source to share


1 answer


This is a duplicate of sails.js Use session parameter in model , so the answer is no. However, you have several options. You can set this value in the policy, or you can overwrite the drawing actions for this. I also require the session user to connect to all models and do it like this.

For example, this in the policy will set the userId in any drawing creation project



req.query.userId = req.session.userId; 

      

0


source







All Articles