how to insert data in mysql table using sequeilze in express.js

时间:2018-02-21 11:49:31

标签: mysql node.js express sequelize.js

I try to insert data data in mysql table with sequelize but it insert null instead of value

i m trying following below code in my file userController.js

var db = require('../../../../config/db');
var bcrypt = require('bcryptjs');
TBL_USERS = db.connection.define('users');
var usersController = {
register : function(req,res,next){
      db.connection.sync().then(function(){
        TBL_USERS.create({
            username:req.body.username,
            email:req.body.email,
            password:bcrypt.hashSync(req.body.password),
            role_id:2
        }).then(function(insertResult){
                //console.log("INSERT...............",insertResult);
                let send_data = {success:true,status:200,message:"success",data:insertResult};
                return res.json(send_data);
        })
            .catch(function(err){
                console.error("query error =>",err);
                return ReE(err, 200,' register fail1',err);
            });
        })
        .catch(function(err){
            console.error("sync error =>",err);
            return ReE(err, 200,'register fail2',err);
        });
}

module.exports = usersController;

it return success message , but it only inserted field_id, createdAt and updatedAt

NOTE: i already validate all value with express-validator so i didn't define in

TBL_USERS = db.connection.define('users');

please help me to resolve this

2 个答案:

答案 0 :(得分:0)

您需要使用表的字段名称定义架构

答案 1 :(得分:0)

需要在 users.js 或数据库模型文件中定义数据类型。将以下两行添加为波纹管

username: DataTypes.STRING,
password: DataTypes.STRING

没有理由不工作。谢谢