2015-04-03 65 views
3

这是sails.js中的一个模型。sails.js beforeCreate方法仅接收需要设置为true的模型属性

module.exports = { 

    attributes: { 
     name: { 
      type: "string" 
     }, 
     email: { 
      type: "email", 
      required: true 
     }, 
     password: { 
      type: "string" 
     } 
    }, 

    beforeCreate: function(values, next) { 
     console.log(values); //logs {email:"[email protected]"} 
     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功能密码的一些加密的,当然我会需要密码,可以用它现在继续,但如何公正的情况下需要对此进行管理可选值出现?

+0

你添加了模型与休息API或从控制器?您是否发送所有字段或从您的正面发送电子邮件? – jaumard 2015-04-03 16:03:56

+0

您是否尝试使用beforeValidate方法?要查看它是不同的 – jaumard 2015-04-03 16:04:09

+0

@ jaumard,我正在构建一个没有视图的REST API,此模型绑定到名为/ auth/signup的路由。我正在通过高级休息控制台Chrome应用程序进行POST,是的,我发布了所有字段。 – codin 2015-04-03 16:07:07

回答

1

我发现上述问题的原因, 在我的控制,我在做一个创造纪录,但纪录我创建只包含一个领域,即email见下图:

Users.create({email:req.body.email}).exec(function(err, user){ 
    // user created 
}); 

模型直接映射到被插入到数据库中的对象,所以内部的sails删除/忽略不存在的字段。 要不删除这些空字段,您可能必须在您的模型中设置schema:true

相关问题