Sails js 不应该 return 密码和电子邮件
Sails js should not return password and email
我正在尝试在 sails js 中创建 CRUD 应用程序,并且我能够 post 数据到我的数据库,我注意到当我成功插入数据时 sails return 整个对象。但是,如果我们不希望某些字段响应,那么我们如何限制它。请帮忙谢谢。
module.exports = {
attributes : {
username : {
type: 'string',
required: true
},
password : {
type: 'string',
required: true
},
email : {
type: 'string',
required: true,
unique: true
}
},
toJson: function() {
var obj = this.toObject();
delete obj.password;
return obj;
},
beforeCreate: function(attribute, callback) {
console.log(attribute.password);
require('bcrypt').hash(attribute.password, 10, function(err, encryptedPassword) {
sails.log(err);
attribute.password = encryptedPassword;
sails.log(encryptedPassword);
callback();
});
}
};
我认为模型通过 sails 默认 REST api 的响应在返回之前通过 .toJSON
运行它们,所以你这样做是正确的。
但是,您可能遇到大小写问题,例如您应该使用大写而不是 .toJson
来定义 .toJSON
。尝试进行该切换,看看是否能解决您的问题。
更新
听起来这并不能解决您的问题。来自 here 的 sails 文档说:
The real power of toJSON relies on the fact every model instance sent out via res.json is first passed through toJSON. Instead of writing custom code for every controller action that uses a particular model (including the "out of the box" blueprints), you can manipulate outgoing records by simply overriding the default toJSON function in your model. You would use this to keep private data like email addresses and passwords from being sent back to every client.
这听起来很像我们正在尝试做的事情,所以这可能是一个 sails 错误。也许它适用于 find
,但不适用于 create
。仅查找现有用户时是否返回该密码?
如果必须,解决此问题的可靠方法是覆盖 UserController 中的默认创建操作:
create: function(req, res) {
User.create(req.body).exec(function(err, user) {
if (err) {
return res.json(err);
}
// explicitly call your own toJSON() to be sure
return res.send(user.toJSON());
});
},
这并不理想,尤其是当您有许多模型属性要隐藏在许多 api 调用中时。但它会完成工作。
@arbuthnott 在上面部分正确——你确实需要 toJSON
而不是 toJson
——但更重要的是,该函数需要进入 内部 attributes
字典,因为是实例方法:
attributes : {
username : {
type: 'string',
required: true
},
password : {
type: 'string',
required: true
},
email : {
type: 'string',
required: true,
unique: true
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
}
password: { type: 'string', required: true, protected: true }
你也可以这样做。
password: { type: 'string', required: true, protected: true }
protected:true
现在已在 sails v1.0
上弃用
你可以用 customToJSON
customToJSON: function() {
// Return a shallow copy of this record with the password and ssn removed.
return _.omit(this, ['password', 'ssn'])
}
我正在尝试在 sails js 中创建 CRUD 应用程序,并且我能够 post 数据到我的数据库,我注意到当我成功插入数据时 sails return 整个对象。但是,如果我们不希望某些字段响应,那么我们如何限制它。请帮忙谢谢。
module.exports = {
attributes : {
username : {
type: 'string',
required: true
},
password : {
type: 'string',
required: true
},
email : {
type: 'string',
required: true,
unique: true
}
},
toJson: function() {
var obj = this.toObject();
delete obj.password;
return obj;
},
beforeCreate: function(attribute, callback) {
console.log(attribute.password);
require('bcrypt').hash(attribute.password, 10, function(err, encryptedPassword) {
sails.log(err);
attribute.password = encryptedPassword;
sails.log(encryptedPassword);
callback();
});
}
};
我认为模型通过 sails 默认 REST api 的响应在返回之前通过 .toJSON
运行它们,所以你这样做是正确的。
但是,您可能遇到大小写问题,例如您应该使用大写而不是 .toJson
来定义 .toJSON
。尝试进行该切换,看看是否能解决您的问题。
更新
听起来这并不能解决您的问题。来自 here 的 sails 文档说:
The real power of toJSON relies on the fact every model instance sent out via res.json is first passed through toJSON. Instead of writing custom code for every controller action that uses a particular model (including the "out of the box" blueprints), you can manipulate outgoing records by simply overriding the default toJSON function in your model. You would use this to keep private data like email addresses and passwords from being sent back to every client.
这听起来很像我们正在尝试做的事情,所以这可能是一个 sails 错误。也许它适用于 find
,但不适用于 create
。仅查找现有用户时是否返回该密码?
如果必须,解决此问题的可靠方法是覆盖 UserController 中的默认创建操作:
create: function(req, res) {
User.create(req.body).exec(function(err, user) {
if (err) {
return res.json(err);
}
// explicitly call your own toJSON() to be sure
return res.send(user.toJSON());
});
},
这并不理想,尤其是当您有许多模型属性要隐藏在许多 api 调用中时。但它会完成工作。
@arbuthnott 在上面部分正确——你确实需要 toJSON
而不是 toJson
——但更重要的是,该函数需要进入 内部 attributes
字典,因为是实例方法:
attributes : {
username : {
type: 'string',
required: true
},
password : {
type: 'string',
required: true
},
email : {
type: 'string',
required: true,
unique: true
},
toJSON: function() {
var obj = this.toObject();
delete obj.password;
return obj;
}
}
password: { type: 'string', required: true, protected: true }
你也可以这样做。
password: { type: 'string', required: true, protected: true }
protected:true
现在已在 sails v1.0
你可以用 customToJSON
customToJSON: function() {
// Return a shallow copy of this record with the password and ssn removed.
return _.omit(this, ['password', 'ssn'])
}