SailsCasts用户模型验证

时间:2015-04-02 03:25:11

标签: node.js sails.js

我正在通过Ponzi Coder遵循SailsCasts Youtube视图创建一个注册表单,但我遇到了用户模型验证的问题。如果我拿出验证规则,我可以提交表单,但是当我将规则重新放入时,我会收到以下警告。另外,当我能够提交它时,只返回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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

你需要输入&#34; name&#34;您的意见如下:

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

成为

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

所有输入都相同。别忘了把#34; name&#34;小写,如你的模型。

答案 1 :(得分:0)

我能解决我的问题。我修改了http.js中的中间件。在我的情况下,bodyParser中间件在订单中丢失。当我将护照与sailsjs整合在一起时,我可能已经这样做了。一旦我添加了&#39; bodyParser&#39;,我的帖子请求就开始获得预期的参数。

希望这有助于某人。

in config/http.js

order: [
  'startRequestTimer',
  'cookieParser',
  'session',
  'bodyParser',
  'passportInit',
  'passportSession',
  '$custom',
  'router',
  'www',
  'favicon',
  '404',
  '500'
]