【问题标题】:Binding Sails.js route to model blueprint action via request body通过请求正文将 Sails.js 路由绑定到模型蓝图操作
【发布时间】:2015-12-17 10:51:07
【问题描述】:

我正在使用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 参数发送数据的情况下使其工作?

【问题讨论】:

  • 你能告诉我们你发布数据的页面(表单),javascript吗?
  • 我正在使用 REST 客户端邮递员 (getpostman.com) 发出此请求

标签: node.js routing sails.js


【解决方案1】:

@num8er 的回答对我来说很有见地(作为一个新手sailsjs/node 程序员),但我的问题实际上是由流氓pm2 实例引起的。

我发出的请求被发送到一个流氓实例,我编写的路由/更改没有反映在代码中。希望这对那里的人有所帮助。

【讨论】:

    【解决方案2】:

    标准方式:

    这是你的表格:

    <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

    【讨论】:

    • 我实际上不是通过表单发出请求,而是通过 REST 客户端发出请求
    • 阅读最后几句话
    • 嗯,我已取消注释该行,但仍然没有。这是我的文件的样子:infinit.io/_/nxNmm9w 和我从服务器得到的响应:Cannot GET /user/create
    • 我很乐意提供帮助,但需要检查、调试。将我添加到 Skype:anarjafarov 我会帮助你
    • 谢谢@num8er!我会在一两个小时内加你。
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多