SailsCasts User model validation

我的梦境 提交于 2019-12-07 22:15:39

问题


I am creating a sign up form by following the SailsCasts Youtube view by Ponzi Coder but I am having issues with the User model validation. If I take out the validation rules I am able to submit the form but I get the warnings below when I put the rules back in. Also, when I am able to submit it, only the ID and the CreatedAt are returned, nothing else:

{
  "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"
      }
    ]
  }
}

Below is my User model:

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'
  },
 
}
};

And below is my 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);
	});	
	}
};

Below is my signup form:

<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>

回答1:


You need to put "name" attribut on your inputs :

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

become

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

Same for all your inputs. Don't forget to put "name" in lower case like your model.




回答2:


I was able to get around my issue. I had modified the middlewares in my http.js. in my case the bodyParser middleware was missing in the order. I might have done that when I integrated passport with sailsjs. Once I added back 'bodyParser', my post requests started getting the expected params.

Hope this helps someone.

in config/http.js

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


来源:https://stackoverflow.com/questions/29404485/sailscasts-user-model-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!