2015-04-02 73 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' 
]