如何在 sails.js 生命周期回调中访问请求对象?
How to access request object in sails.js lifecycle callbacks?
假设我有这个模型:
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();
}
您可以通过两种方式完成。
首先是在控制器中添加该数据。像
// /api/controllers/mycontroller.js
module.exports = {
new: function(req, res) {
if (typeof req.user.id !== 'undefined') {
req.body.createdBy = req.user.id; // req.body or req.params
}
MyModel.create(req.body /* ... */)
}
}
如果您使用 MyModel
进行大量数据操作,可能会很烦人。因此,您可以将静态方法添加到您的模型中,以使用用户 ID 保存它。类似于:
// /api/models/myModel.js
module.exports = {
attributes: {/* ... */},
createFromRequest: function(req, cb) {
// do anything you want with your request
// for example add user id to req.body
if (typeof req.user.id !== 'undefined') {
req.body.createdBy = req.user.id;
}
MyModel.create(req.body, cb);
}
}
并在您的控制器中使用它
// /api/controllers/mycontroller.js
module.exports = {
new: function(req, res) {
MyModel.createFromRequest(req, function(err, data) {
res.send(data);
});
}
}
由于请求超出了 ORM 的范围,我猜想我的方法是错误的,我需要将 createdBy 数据添加到中间件中的 req.body。但由于不会对每个请求都这样做,我猜最好通过策略来做到这一点。像这样:
PostController: {
'*': ['passport', 'sessionAuth'],
create: ['passport', 'sessionAuth',
function (req, res, next) {
if (typeof req.body.createdBy === 'undefined') {
req.body.createdBy = req.user.id;
}
next();
}
]
}
这样我就不需要覆盖蓝图了。
假设我有这个模型:
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();
}
您可以通过两种方式完成。
首先是在控制器中添加该数据。像
// /api/controllers/mycontroller.js
module.exports = {
new: function(req, res) {
if (typeof req.user.id !== 'undefined') {
req.body.createdBy = req.user.id; // req.body or req.params
}
MyModel.create(req.body /* ... */)
}
}
如果您使用 MyModel
进行大量数据操作,可能会很烦人。因此,您可以将静态方法添加到您的模型中,以使用用户 ID 保存它。类似于:
// /api/models/myModel.js
module.exports = {
attributes: {/* ... */},
createFromRequest: function(req, cb) {
// do anything you want with your request
// for example add user id to req.body
if (typeof req.user.id !== 'undefined') {
req.body.createdBy = req.user.id;
}
MyModel.create(req.body, cb);
}
}
并在您的控制器中使用它
// /api/controllers/mycontroller.js
module.exports = {
new: function(req, res) {
MyModel.createFromRequest(req, function(err, data) {
res.send(data);
});
}
}
由于请求超出了 ORM 的范围,我猜想我的方法是错误的,我需要将 createdBy 数据添加到中间件中的 req.body。但由于不会对每个请求都这样做,我猜最好通过策略来做到这一点。像这样:
PostController: {
'*': ['passport', 'sessionAuth'],
create: ['passport', 'sessionAuth',
function (req, res, next) {
if (typeof req.body.createdBy === 'undefined') {
req.body.createdBy = req.user.id;
}
next();
}
]
}
这样我就不需要覆盖蓝图了。