0

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

1
  • Are you using body parser ? Try to log req.body and see if your expected values show up Commented Mar 4, 2018 at 18:52

2 Answers 2

1

Need to define data types in users.js or your database model file. add two lines as bellow

username: DataTypes.STRING,
password: DataTypes.STRING

There is no reason to not working. thanks

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

Comments

0

You need to define schema with field name of table

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.