sails.js - how can I acess session data in Model hook beforeCreate

匆匆过客 提交于 2019-11-30 21:02:27

Are you sure a lifecycle callback is the right place to do it? Because it's really not. What if tomorrow you'll need to use your model in a CLI task or something else sessionless? Besides, with the associations API coming, there will be, probably, a more elegant way to do it, but still outside of the model. So, for now I would just treat your reference as a simple value, set it in the action (from where you can access req.session) and pass to the constructor along with other properties, something like:

...
// Controller code
module.exports = {
  index: function(req, res) {
    // Whatever gets you the values...

    values.owner = req.session.user_id;
    Model.create(values, function(err, model) {...});
  },
  // Other actions
}
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!