1

I'm using Linux and, I'm trying to insert my Textbox values into mysql via sails.js. When I surfing the net, I didn't get any clear answer.

I have attached My Controller for create function

module.exports = {

create :function(req,res){

    if(req.method=="POST"&&req.param("User",null)!=null) 
    {

        var insert = "INSERT INTO User VALUES("+req.params.userId+",'"+req.params.loginName+"','"+req.params.userName+"','"+req.params.password+"','"+req.params.userMail+"')";

        User.query(insert,function(err,record){
        if(err)
        {
            console.log("Error");
        }
        else
        {

            console.log(record);
            res.redirect('User/index');

        }

        }); 
    }   

},

this is my create.ejs

<form action="/user/create" method="POST">
    <table>
        <tr><td>UserId:<td><input type="text" name="User[userId]"><br/>
        <tr><td>LoginName:<td><input type="text" name="User[loginName]"><br/>
        <tr><td>UserName:<td><input type="text" name="User[userName]"><br/>
        <tr><td>Password:<td><input type="text" name="User[password]"><br/>
        <tr><td>UserMail:<td><input type="text" name="User[userMail]"><br/>
        <tr><td><td><input type="submit" value="Register">
</form>

connection.js is:

module.exports.connections = {

mysql: {
    module    : 'sails-mysql',
    host      : 'localhost',
    port      : 3306,
    user      : 'root',
    password  : 'assyst',
    database  : 'User' 

},  

};

How to do basic crud operations in sails.js with mysql.

1

2 Answers 2

1

Expect if you create User API by sails generate api user it will automatically UserController.js at api/controllers and User.js at api/models.

Modify User.js at your api/models/User.js to

module.exports = {

  attributes  : {
    userId      : {type: 'string'},
    loginName   : {type: 'string'},
    userName    : {type: 'string'},
    password    : {type: 'string'},
    userMail    : {type: 'string'}
  }
};

By default, it expose Blueprint API and you can POST something to user model by:

<form action="/user" method="POST">
    <table>
        <tr><td>UserId:<td><input type="text" name="userId"><br/>
        <tr><td>LoginName:<td><input type="text" name="loginName"><br/>
        <tr><td>UserName:<td><input type="text" name="userName"><br/>
        <tr><td>Password:<td><input type="text" name="password"><br/>
        <tr><td>UserMail:<td><input type="text" name="userMail"><br/>
        <tr><td><td><input type="submit" value="Register">
</form>

That's the basic CRUD at Sails.

Sign up to request clarification or add additional context in comments.

1 Comment

@Martin great to hear that. Don't forget to choose an answer to avoid misleading to other people who think this topic is still need an answer.
0

Here is my insert operation in sails.js with Mysql

Insert:function(req,res)
{
    if(req.method=="POST")
        {
            console.log("Post");
            var userId = req.param("userId");
            var userName = req.param("userName");
            var loginName =req.param("loginName");
            var password =req.param("password");
            var userMail =req.param("userMail");
            console.log(userName);

            var insert = "INSERT INTO User(UserId,LoginName,UserName,password,UserMail) VALUES("+userId+",'"+loginName+"','"+userName+"','"+password+"','"+userMail+"')";

            User.query(insert,function(err,record)
            {

                if(err)
                {
                    console.log(err);
                }
                else
                {
                    console.log(record);
                    res.redirect('User/index');
                }
            });

        }
        else
        {
            res.render("create");
        }
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.