Zaki Aziz
Zaki Aziz

Reputation: 3852

Binding Sails.js route to model blueprint action via request body

I'm using sails.js v0.11.1. In my routes.js file I have this route written:

'POST /user' : {
    model:     'user',
    blueprint: 'create'
},

I'm sending a POST request (via postman) to /user with this in the body:

{
    userName  : "firstUser",
    password  : "secretPwd5779", 
    firstName : "William",
    lastName  : "Askey"
}

but I'm getting this response:

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

According to the Sails.js documentation I've written the route correctly.

Making this request via URL parameters (i.e. making the request to /user?userName=firstUser&password=secretPwd5779&firstName=William&lastName=Askey works just fine. How can I get this to work without sending data via URL parameters?

Upvotes: 1

Views: 392

Answers (2)

Zaki Aziz
Zaki Aziz

Reputation: 3852

@num8er's answer was insightful for a me (as a novice sailsjs/node programmer) but my issue was actually caused by rogue pm2 instances.

The requests I was making was being sent to a rogue instance where the routes/changes I've written did not reflect in the code. Hope this helps someone out there.

Upvotes: 2

num8er
num8er

Reputation: 19372

Standard way:

This is Your form:

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

and this is frontend js part (jquery is required):

$(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.');
        }
      });
  });
});

But You're sending json body, so have to enable middleware that will convert json body to request.body.
it's called body-parser. see screenshot: http://joxi.ru/752aJJbhj45DA0

Upvotes: 2

Related Questions