sails.js beforeCreate 方法仅接收 required 设置为 true 的模型属性
sails.js beforeCreate method receives only the model properties on which required is set to true
这是sails.js中的模型。
module.exports = {
attributes: {
name: {
type: "string"
},
email: {
type: "email",
required: true
},
password: {
type: "string"
}
},
beforeCreate: function(values, next) {
console.log(values); //logs {email:"mail@someplace.com"}
console.log(values.email); // logs the email id sent via post
console.log(values.password); // logs undefined is required is set to false, If required is set to true, password will log properly.
next();
}
};
我计划在 beforeCreate 函数中对密码进行一些加密,当然我需要密码并且现在可以继续使用它,但是如何管理可选值的密码以备不时之需?
我找到了上述问题的原因,
在我的控制器中,我正在创建一条记录,但我正在创建的记录只包含一个字段,即 email
见下文:
Users.create({email:req.body.email}).exec(function(err, user){
// user created
});
该模型直接映射到插入数据库的对象,因此在内部航行 removes/ignores 不存在的字段。
要不删除这些空字段,您可能必须在模型中设置 schema:true
。
这是sails.js中的模型。
module.exports = {
attributes: {
name: {
type: "string"
},
email: {
type: "email",
required: true
},
password: {
type: "string"
}
},
beforeCreate: function(values, next) {
console.log(values); //logs {email:"mail@someplace.com"}
console.log(values.email); // logs the email id sent via post
console.log(values.password); // logs undefined is required is set to false, If required is set to true, password will log properly.
next();
}
};
我计划在 beforeCreate 函数中对密码进行一些加密,当然我需要密码并且现在可以继续使用它,但是如何管理可选值的密码以备不时之需?
我找到了上述问题的原因,
在我的控制器中,我正在创建一条记录,但我正在创建的记录只包含一个字段,即 email
见下文:
Users.create({email:req.body.email}).exec(function(err, user){
// user created
});
该模型直接映射到插入数据库的对象,因此在内部航行 removes/ignores 不存在的字段。
要不删除这些空字段,您可能必须在模型中设置 schema:true
。