Kenny
Kenny

Reputation: 256

Node.js Sails html data using "POST" method can not pass to controller

I am new to MVC.

When I want to pass the form data from ejs file to controller, it does not work.

Create.ejs file

<!--create.ejs-->
<h2 class="col-sm-offset-1">Person Create form</h2>

<form action="/person/create" method="POST" class="form-horizontal">
<div class="form-group">
    <label class="control-label col-sm-2">Name: </label>
    <div class="col-sm-8">
        <input class="form-control" name="Person[name]" type="text">
    </div>
</div>

<div class="form-group">
    <label class="control-label col-sm-2">Age: </label>
    <div class="col-sm-8">
        <input class="form-control" name="Person[age]" type="number"
        min="0" max="120">
    </div>
</div>

<button class="btn btn-default col-sm-offset-2" type="submit" value="ADD">Submit</button>

PersonController.js

module.exports = {
create: function(req, res) {
if (req.method == "POST") {
    Person.create(req.body.Person).exec( function(err, model) {
        return res.send("Successfully Created!");
    });
} else {
    return res.view('person/create');
}
},

The result is that it cant get inside the (req.method == "POST") condition. Give me 404 error.

Upvotes: 1

Views: 960

Answers (2)

Eli Peters
Eli Peters

Reputation: 420

A 404 is a not found result.

When POSTing ensure you are targeting your Controller functions try:

'POST /person/create': 'person/PersonController.create',

Though why do you have a subfolder named person when you are using a controller?

Using Actions

In Sails v1.0 you would separate your controller functions into more manageable actions. A sub folder api/controllers/person makes more sense now.

Your routes.js file would read 'POST /person/create': {action: 'person/create'},

For simplicity I have removed req.body.Person as an array...

<form action="/person/create" method="POST" class="form-horizontal">

  <input type="hidden" name="_csrf" value="<%= _csrf %>" />

  <div class="form-group">
    <label class="control-label col-sm-2">Full Name: </label>
    <div class="col-sm-8">
      <input class="form-control" name="fullName" placeholder="John Johnson" type="text">
    </div>
  </div>

  <div class="form-group">
    <label class="control-label col-sm-2">Age: </label>
    <div class="col-sm-8">
      <input class="form-control" name="age" type="number" min="0" max="120">
    </div>
  </div>

  <button class="btn btn-default col-sm-offset-2" type="submit" value="ADD">Submit</button>

</form>

then your async function would be like...

module.exports = {


      friendlyName: 'Create Person.',
      description: 'Creating a new person.',


      exits: {

        success: {
          description: 'Person Created successfully.',
          viewTemplatePath: 'person/review',
        }

      },

      fn: async function (inputs, exits) {
        
        if (!this.req.me) throw {redirect: '/'};   

        let params = this.req.allParams();

        let person = await Person.create(params).fetch();
        
        return exits.success({person:person});

      }
}

Upvotes: 3

Raqem
Raqem

Reputation: 424

Like @Tejashwi suggested you need to change your route to a POST.

    'POST  /api/v1/create':   { action: 'create/person-create-form' },

Upvotes: 1

Related Questions