假设我有这种模式:如何访问sails.js生命周期回调中的请求对象?
module.exports = {
attributes: {
title: {
type: 'string',
required: true
},
content: {
type: 'string',
required: true
},
createdBy: {
type: 'string',
required: true
}
}
}
我需要设置当前用户ID到模型的createdBy属性。我想我可以用beforeValidate生命周期回调来做到这一点,但我无法访问存储当前用户的请求对象。有没有办法访问它,或者我应该以其他方式解决这个问题吗?
我想这没有成功:
beforeValidate: function (values, next) {
var req = this.req; // this is undefined
values.createdBy = req.user.id;
next();
}
我很惊讶更多的人不推荐这个。总是将其添加到控制器中是对蓝图目标的反模式。政策使这更容易。 – 2015-07-17 00:37:18
好的,但是我怎么能够通过这个策略访问生命周期回调中的req? – pie6k 2016-01-05 08:15:07
太棒了!这解决了我的问题! – 2016-02-21 13:42:25