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
});
模型与插入数据库的对象直接映射,因此内部帆会删除/忽略不存在的字段。若要不删除这些空字段,您可能必须在模型中设置schema:true
。
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- sails.js beforeCreate方法只接收required设置为true的模型属性
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作
- 设置为负数的变量计算结果为true
- 如何使函数将属性值从 FALSE 设置为 TRUE
- Ext JS 5.1:当可折叠为true时,无法对面板进行标题设置
- 设置“;将标签视为文本“;通过脚本将Google电子表格图表的值设置为true
- 谷歌地图:想在设置draggable为true后直接拖动地图
- 否'访问控制允许来源'当跨域设置为true时,请求的资源错误中存在标头
- 在unslider.js中设置true或false
- 如果选定,如何将所选选项设置为 true
- 使用函数将 V-show 设置为 true 或 false
- 当网格视图设置为 true 时,Jqgrid 在 IE9 中显示“正在加载”
- locaction.reload(true) 会导致强制获取 - 设置 .src 时如何执行此操作
- MongoDB:确保只有一个文档可以将字段设置为 true
- 将 CKEDITOR.config.contentAllow 设置为 true 仍会删除类
- 当我在 Amazon S3 中将安静设置为 true 以在 deleteObjects() 方法中分析安静模式时会发生什
- 当函数在 Javascript 中将其设置为 true 时,布尔值返回 false
- 在验证完成之前,AngularJS 表单$valid设置为 true
- 使用复选框和angular为localStorage设置true/false值