2015-04-02 75 views
2

我正在通过Ponzi Coder的SailsCasts Youtube视图创建注册表单,但我遇到了用户模型验证方面的问题。如果我拿出验证规则,我可以提交表单,但是当我重新插入规则时,我会收到以下警告。另外,当我能够提交时,只返回ID和CreatedAt,没有别的:SailsCasts用户模型验证

{ 
 
    "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 = { 
 
\t 
 
schema: true, 
 
\t 
 
    attributes: { 
 
    name: { 
 
\t type: 'string', 
 
\t required: true 
 
\t }, 
 
    
 
    email: { 
 
\t type: 'string', 
 
\t email: true, 
 
\t unique: true, 
 
\t required: true 
 
    }, 
 
    password: { 
 
\t type: 'string', 
 
\t required: true 
 
    }, 
 
    encryptedPassword:{ 
 
\t type: 'string' 
 
    }, 
 
    
 
} 
 
};

而下面是我的UserController中:

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

下面是我的注册表格:

<form action="/user/create" method="POST" class="form-horizontal"> 
 
    <div class="control-group"> 
 
\t <label class="control-label" for="inputname">Name</label> 
 
    <div class="controls"> 
 
     <input type="text" id="Name" placeholder="Name"> 
 
    </div> 
 
    </div> 
 
    
 
    <div class="control-group"> 
 
\t <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>

+0

我一直面临同样的问题。我曾尝试使用POSTMAN,并使用和不使用默认的BluePrints。我尝试通过发送params作为form-data或x-www-form-urlencoded或raw以及Content-type头设置为application/json来尝试POSTMAN。但没有任何工作。唯一可行的是,如果我将参数作为查询字符串参数提供,我不打算将这些参数用于POST请求。 – 2015-05-09 22:29:13

回答

2

你需要穿上 “名” attribut您输入:

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

成为

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

同样为您的所有投入。不要忘记像你的模型一样用小写字母来表示“名字”。

0

我能解决我的问题。我修改了我的http.js中的中间件。在我的情况下,bodyParser中间件在顺序中缺失。当我将护照与sailsjs结合起来时,我可能会这样做。一旦我添加了“bodyParser”,我的帖子请求就开始获得预期的参数。

希望这可以帮助别人。

in config/http.js 

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