node.js - SailsCasts 用户模型验证

标签 node.js sails.js

我正在按照 Ponzi Coder 的 SailsCasts Youtube View 创建注册表单,但我在用户模型验证方面遇到问题。如果我取出验证规则,我可以提交表单,但当我重新放入规则时,我会收到以下警告。此外,当我能够提交它时,仅返回 ID 和 CreatedAt,没有其​​他内容:

{
  "error": "E_VALIDATION",
  "status": 400,
  "summary": "3 attributes are invalid",
  "model": "User",
  "invalidAttributes": {
    "name": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ],
    "email": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "email",
        "message": "\"email\" validation rule failed for input: null"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ],
    "password": [
      {
        "rule": "string",
        "message": "`undefined` should be a string (instead of \"null\", which is a object)"
      },
      {
        "rule": "required",
        "message": "\"required\" validation rule failed for input: null"
      }
    ]
  }
}

以下是我的用户模型:

module.exports = {
	
schema: true,
	
  attributes: {
  name: {
	type: 'string',
	required: true
	  },
  
  email: {
	  type: 'string',
	  email: true,
	  unique: true,
	  required: true
  },
  password: {
	  type: 'string',
	  required: true
  },
  encryptedPassword:{
	  type: 'string'
  },
 
}
};

下面是我的 UserController:

module.exports = {
	// Gets the view: signup.ejs
	'signup': function(req, res){
		res.view();
	},
	
	//Creates a user with the info sent from the 
	//signup form in signup.ejs
	create: function(req, res, next){
	User.create (req.params.all(), function userCreated(err, user){
		//If there is an error
		if(err) return next(err);
		
		//After the user is successfully created
		//redirect user to the show action
		res.json(user);
	});	
	}
};

下面是我的注册表单:

<form action="/user/create" method="POST" class="form-horizontal">
  <div class="control-group">
	<label class="control-label" for="inputname">Name</label>
    <div class="controls">
      <input type="text" id="Name" placeholder="Name">
    </div>
  </div>
  
  <div class="control-group">
	<label class="control-label" for="inputEmail">Email</label>
    <div class="controls">
      <input type="text" id="Email" placeholder="Email">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">Password</label>
    <div class="controls">
      <input type="password" id="Password" placeholder="Password">
    </div>
  </div>
  
  <div class="control-group">
    <label class="control-label" for="inputRetypePassword">Retype Password</label>
    <div class="controls">
      <input type="password" id="RetypePassword" placeholder="Retype Password">
    </div>
  </div>
  <div class="control-group">
    
      <button type="submit" class="btn">Sign up</button>
    
  </div>
  <input type="hidden" name="_csrf" value="<%= _csrf %>"/>
</form>

最佳答案

您需要在输入中添加“名称”属性:

<input type="text" id="Name" placeholder="Name">

成为

<input type="text" id="Name" name="name" placeholder="Name">

您的所有输入都相同。不要忘记将“名称”写成小写,就像您的型号一样。

关于node.js - SailsCasts 用户模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29404485/

相关文章:

mysql - 使用 sails 作为 REST API 的 Sails passport js 集成

node.js - 使用 sails 时是否需要在 package.json 中显式 grunt

javascript - sails.js 中 Controller 与 typescript (或不 typescript )之间的继承

node.js - 在重新启动之间保存 readline CLI 命令历史记录

mongodb - Mongoose:填充填充字段

javascript - expressjs中的路由错误

node.js - Swagger 生成器无法识别 Controller

javascript - 从 Express 后端下载 .csv 到 Angularjs

javascript - 如何使 sails-linker 在子/子页面中加载不同的资源?

node.js - Sails.js Redis 如何流式传输数据?