0

I'm just learning about NodeJS and am I'm trying to create data using sequelize Postgres. but I've got some error

bcrypt UnhandledPromiseRejectionWarning: Error: data and salt arguments required. 

Here is my code model user.js

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of the Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  User.init({
    full_name: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: {
          msg: 'Nama tidak boleh kosong'
        }
      }
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        isEmail: true,
        notEmpty: {
          msg: 'Email tidak boleh kosong'
        },
        unique: {
          args: true,
          msg: 'Email address already in use!'
        }
      }
    },
    username: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: {
          msg: 'Username tidak boleh kosong'
        }
      }
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: {
          msg: 'Password tidak boleh kosong'
        }
      }
    },
    profile_image_url: {
      type: DataTypes.STRING,
      allowNull: true,
      defaultValue: 'www.image.com',
      validate: {
        notEmpty: {
          msg: 'Url tidak boleh kosong'
        }
      }
    },
    age: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        notEmpty: {
          msg: 'Umur tidak boleh kosong'
        }
      }
    },
    phone_number: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notEmpty: {
          msg: 'No telp tidak boleh kosong'
        }
      }
    }
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};

I've been looking for answers from several other sources, but I haven't found it yet. Here is my controller createUser.js.

const {
  User
} = require('../../models/user');
const bcrypt = require('bcrypt');

module.exports = async (req, res) => {
  const {
    full_name,
    email,
    username,
    password,
    age,
    phone_number
  } = req.body;
  const hash = await bcrypt.hash(password, 10);

  await User.create({
    full_name,
    email,
    username,
    password: hash,
    age,
    phone_number
  }).then(success => {
    return res.status(201).json({
      status: 'success',
      message: 'Success register',
      data: success
    })
  }).catch(error => {
    return res.status(400).json({
      status: 'error',
      message: error.message
    });
  });
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175

0 Answers0