Sails.js: cannot do POST for inserting into mongoDB

546 views
Skip to first unread message

IVYtony smth

unread,
Nov 15, 2014, 7:53:47 AM11/15/14
to sai...@googlegroups.com

I am trying to write a toy program for submitting form and inserting into mongodb, but I keep getting DB error. Let me paste the relevant code here, and I hope to get some help.

I am using Sails ver 0.10.5 and the latest mongo 2.6.5, and my I run node on my Mac OSX 10.9:

Model: Employee.js

module.exports = {

  attributes: {
                  name: {
                            type: "string",
                            required: true
                        },
                  email: {
                             type: "string",
                             required: true
                         },
                  password: {
                                type: "string",
                                required: true
                            }
  },
beforeCreate: function(values, next) {
      next();
    }
};

route.js:

module.exports.routes = {

  '/registeremployee': {
    controller: 'employee',
    action: 'register'
  },

  '/listemployees': {
    controller: 'employee',
    action: 'list_all'
  }
};

EmployeeController.js

module.exports = {

  index: function(req, res) {
    res.send(200, {title: "employee index page"});
  },

  list_all: function(req, res) {

    Employee.find().exec(function(err, employee) {

      if (err) {
        res.send(500, {title: 'error retrieving users'});
      } else {
        res.send(200, {'employees': employee});
      }
    });
  },

  register: function(req, res) {

    if (req.method == "GET") {
      res.view({title: "Form for registering employees"});
    } else if (req.method == "POST") {

      var username = req.param("username");
      var password = req.param("password");
      var email = req.param("email");

      console.log("saving the username: " + username);  //username printed as 'undefined'

      Employee.create({username: username, password: password, email: email}).exec(function(error, employee) {
        if (error) {
          console.log('error');
          res.send(500, {error: "DB error!"});
        } else {
          console.log('error');
          res.send(200, employee);

          console.log("saved employee: " + employee.username);
        }
      });
    }

  }
};

Lastly, the register.ejs template file:

<a href="/listemployees">List All Users</a>
<h2>Form - Create a User</h2>
<form action="/registeremployee" method="POST">
  <table>
    <tr><td>Name</td><td><input type=”text” name=”username” required></td></tr>
    <tr><td>Password</td><td><input type=”password” name=”password” required></td></tr>
    <tr><td>Email</td><td><input type=”email” name=”email” required></td></tr>
    <tr><td></td><td><input type="submit"></td>
  </table>
</form>

It looks to me that the form does not submit data, as the parameters are printed as undefined/null in my controller.

I have this in my connections.js:

  mongo: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    user: '',
    password: '',
    database: 'sailsApp1'
  }

Ambroise Dhenain

unread,
Nov 15, 2014, 8:04:52 AM11/15/14
to IVYtony smth, sai...@googlegroups.com
I think your "registeremployee" is set for GET in your routes.
Try action="/employee/register" instead, just to see if that's it.

Do you have CRSF disabled?

IVYtony smth

unread,
Nov 15, 2014, 8:25:56 AM11/15/14
to sai...@googlegroups.com, ivy...@gmail.com
thanks, but I got the following errors on terminal when submitting the form. I don't understand why req.param() cannot get the values submitted in the form.

Invalid attributes sent to Employee:
 • name
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null
 • email
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null
 • password
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null

Ambroise Dhenain

unread,
Nov 15, 2014, 8:32:33 AM11/15/14
to IVYtony smth, sai...@googlegroups.com
You should console.log(req.params) in your controller, or another method, there are several and I don't remember which one is the bestto display all params.

IVYtony smth

unread,
Nov 15, 2014, 8:51:18 AM11/15/14
to sai...@googlegroups.com, ivy...@gmail.com
I tested printing out all the properties in req.params.all(), and it has all the fields (name, email, and password) in my model, however, the create() method complains all these posted fields are null. I don't understand why:


var model = req.params.all();

for (var prop in model) {
console.log("property value is: " + model[prop]);
}

IVYtony smth

unread,
Nov 15, 2014, 8:52:54 AM11/15/14
to sai...@googlegroups.com, ivy...@gmail.com
and this is the create() method I use:

Employee.create(req.params.all()).exec(function(error, employee) {

if (error) {
return console.log(error);
} else {

res.send(200, employee);
console.log("saved employee: " + employee.name);
}
});

Ambroise Dhenain

unread,
Nov 15, 2014, 8:55:08 AM11/15/14
to IVYtony smth, sai...@googlegroups.com
So, it is not your controller's fault. It's something to do with the model I guess, because your controller seems to get the POST data using req.params.all()
--
Cordialement,

M. Ambroise Dhenain.

IVYtony smth

unread,
Nov 15, 2014, 9:09:00 AM11/15/14
to sai...@googlegroups.com, ivy...@gmail.com
thanks, I 'm just pasting all the detailed errors below, hoping someone could help:

Error (E_VALIDATION) :: 3 attributes are invalid
    at WLValidationError.WLError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:33:18)
    at new WLValidationError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js:20:28)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/validate.js:45:43
    at allValidationsChecked (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:195:5)
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:186:14
    at done (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:135:19)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:32:16
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/validations.js:157:64

Invalid attributes sent to Employee:
 • name
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null
 • email
   • `undefined` should be a email (instead of "null", which is a object)

   • "required" validation rule failed for input: null
 • password
   • `undefined` should be a string (instead of "null", which is a object)
   • "required" validation rule failed for input: null

IVYtony smth

unread,
Nov 15, 2014, 11:35:41 PM11/15/14
to sai...@googlegroups.com
I found something interesting at the differences of model definitions.

When I define my fields in the following format, the POST method of the form and the model works:

name: 'STRING',
email: 'STRING',
password: 'STRING'

However, the POST method fails when I use the object literal format for the fields as following:

name: {
type: 'STRING',
},

email: {
type: 'STRING',
email: true,
},

password: {
type: 'STRING',
},


So could someone help me understand why the object literal format does not work? and how to make it work?
Reply all
Reply to author
Forward
0 new messages