通过请求正文将 Sails.js 路由绑定到模型蓝图操作
Binding Sails.js route to model blueprint action via request body
我正在使用 sails.js v0.11.1。
在我的 routes.js 文件中,我写了这条路线:
'POST /user' : {
model: 'user',
blueprint: 'create'
},
我正在向 /user
发送 POST 请求(通过 postman),正文为:
{
userName : "firstUser",
password : "secretPwd5779",
firstName : "William",
lastName : "Askey"
}
但我收到了这样的回复:
{
"error": "E_VALIDATION",
"status": 400,
"summary": "4 attributes are invalid",
"model": "User",
"invalidAttributes": {
"userName": [
{
"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"
}
],
"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"
}
],
"firstName": [
{
"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"
}
],
"lastName": [
{
"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"
}
]
}
}
根据 Sails.js documentation 我写的路线是正确的。
通过 URL 参数发出此请求(即向 /user?userName=firstUser&password=secretPwd5779&firstName=William&lastName=Askey
发出请求工作得很好。我如何在不通过 URL 参数发送数据的情况下使其工作?
标准方式:
这是您的表格:
<form class="ajax" method="post" action="/user">
<input name="userName">
<input name="password">
<input name="firstName">
<input name="lastName">
<button type="submit">CREATE</button>
</form>
<div id="result"></div>
这是前端 js 部分(需要 jquery):
$(function(){
$('form.ajax').on('submit', function(e){
e.preventDefault();
var method = $(this).attr('method');
var url = $(this).attr('action');
var data = $(this).serializeArray();
$.ajax({
url: url,
method: method,
data: data,
success: function(response) {
$('#result').html(JSON.stringify(response));
},
error: function() {
alert('Error! Try again later.');
}
});
});
});
但是您正在发送 json 正文,因此必须启用将 json 正文转换为 request.body 的中间件。
它被称为正文解析器。看截图:http://joxi.ru/752aJJbhj45DA0
@num8er 的回答对我(作为新手 sailsjs/node 程序员)来说很有见地,但我的问题实际上是由流氓 pm2 实例引起的。
我发出的请求被发送到一个流氓实例,其中我编写的 routes/changes 没有反映在代码中。希望这可以帮助那里的人。
我正在使用 sails.js v0.11.1。 在我的 routes.js 文件中,我写了这条路线:
'POST /user' : {
model: 'user',
blueprint: 'create'
},
我正在向 /user
发送 POST 请求(通过 postman),正文为:
{
userName : "firstUser",
password : "secretPwd5779",
firstName : "William",
lastName : "Askey"
}
但我收到了这样的回复:
{
"error": "E_VALIDATION",
"status": 400,
"summary": "4 attributes are invalid",
"model": "User",
"invalidAttributes": {
"userName": [
{
"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"
}
],
"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"
}
],
"firstName": [
{
"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"
}
],
"lastName": [
{
"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"
}
]
}
}
根据 Sails.js documentation 我写的路线是正确的。
通过 URL 参数发出此请求(即向 /user?userName=firstUser&password=secretPwd5779&firstName=William&lastName=Askey
发出请求工作得很好。我如何在不通过 URL 参数发送数据的情况下使其工作?
标准方式:
这是您的表格:
<form class="ajax" method="post" action="/user">
<input name="userName">
<input name="password">
<input name="firstName">
<input name="lastName">
<button type="submit">CREATE</button>
</form>
<div id="result"></div>
这是前端 js 部分(需要 jquery):
$(function(){
$('form.ajax').on('submit', function(e){
e.preventDefault();
var method = $(this).attr('method');
var url = $(this).attr('action');
var data = $(this).serializeArray();
$.ajax({
url: url,
method: method,
data: data,
success: function(response) {
$('#result').html(JSON.stringify(response));
},
error: function() {
alert('Error! Try again later.');
}
});
});
});
但是您正在发送 json 正文,因此必须启用将 json 正文转换为 request.body 的中间件。
它被称为正文解析器。看截图:http://joxi.ru/752aJJbhj45DA0
@num8er 的回答对我(作为新手 sailsjs/node 程序员)来说很有见地,但我的问题实际上是由流氓 pm2 实例引起的。
我发出的请求被发送到一个流氓实例,其中我编写的 routes/changes 没有反映在代码中。希望这可以帮助那里的人。