0
votes

I am starting my node development server after migrating my models. How ever I get two Errors:

a TypeError and And Error in declaring more than one ENUM DataTypes in my Models.

I have tried changing the type of the other ENUM fields, and it worked if its only one ENUM field, bu wont work if more than one. I don't know how to deal with the Type Error from Sequelize camelize() utility function in the sequelize library.

A:\Portal_project\customer-portal\Server\node_modules\sequelize\lib\utils.js:94
  return str.trim().replace(/[-_\s]+(.)?/g, (match, c) => c.toUpperCase());

TypeError: Cannot read property 'toUpperCase' of undefined at str.trim.replace (A:\Portal_project\customer-portal\Server\node_modules\sequelize\lib\utils.js:94:61) at String.replace () at Object.camelize (A:\Portal_project\customer-portal\Server\node_modules\sequelize\lib\utils.js:94:21) at new HasOne (A:\Portal_project\customer-portal\Server\node_modules\sequelize\lib\associations\has-one.js:43:31) at Function. (A:\Portal_project\customer-portal\Server\node_modules\sequelize\lib\associations\mixin.js:105:25) at Function.Employee.associate (A:\Portal_project\customer-portal\Server\db\models\employee.js:58:14) at Object.keys.forEach.modelName (A:\Portal_project\customer-portal\Server\db\models\index.js:30:19) at Array.forEach () at Object. (A:\Portal_project\customer-portal\Server\db\models\index.js:28:17) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:22:18) at Object. (A:\Portal_project\customer-portal\Server\api\controllers\customerController.js:1:78) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3)

I expect all the sequelize migrations to work, and my development server to start. but I kept getting this Type Error.

here is my Employee models

'use strict';

module.exports = (sequelize, DataTypes) => {
  const Employee = sequelize.define('Employee', {
    employee_name: DataTypes.STRING,
    employee_id: DataTypes.STRING,
    role: {
      type: DataTypes.ENUM,
      values: ['employee','super employee','admin', 'super admin']
    },
    email_address: DataTypes.STRING,
    id_type: DataTypes.STRING,
    id_number: DataTypes.STRING,
    id_expiry_date: DataTypes.DATEONLY,
    date_of_birth: DataTypes.DATEONLY,
    nationality: DataTypes.STRING,
    state_of_origin: DataTypes.STRING,
    phone_number: DataTypes.STRING,
    email: DataTypes.STRING,
    marital_status: DataTypes.STRING,
    home_address: DataTypes.STRING,
    local_government_of_origin: DataTypes.STRING,
    religion: DataTypes.STRING,
    gender: DataTypes.STRING,
    blood_group: DataTypes.STRING,
    disabilty: DataTypes.STRING,
    department: DataTypes.STRING,
    clearance_level: {
      type: DataTypes.ENUM,
      values: ['employee', 'supervisor', 'admin', 'super admin']
    },
    date_of_employment: DataTypes.DATE,
    employment_type: {
      type: DataTypes.ENUM,
      values: ['full tme', 'contract']
    },
    date_of_contract_termination: DataTypes.DATE,
    form_of_id: DataTypes.STRING,
    bank_name: DataTypes.STRING,
    account_name: DataTypes.STRING,
    account_number: DataTypes.STRING,
    bvn: DataTypes.STRING,
    payment_type: DataTypes.STRING,
    salary_amount: DataTypes.DOUBLE,
    spouse_name: DataTypes.STRING,
    spouse_phone_number: DataTypes.STRING,
    spouse_address: DataTypes.STRING,
    next_of_kin_name: DataTypes.STRING,
    next_of_kin_phone_number: DataTypes.STRING,
    next_of_kin_address: DataTypes.STRING

  }, {
    underscored: true,
  });

  Employee.associate = function(models) {
    // associations can be defined here
    Employee.hasOne(models.IDCardRequest, {
      as: 'employee_id_request'
    })
  };

  Employee.removeAttribute('id');

  return Employee;
};


and the migration file

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Employees', {
      employee_name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      employee_id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.STRING
      },
      role: {
        allowNull: false,
        type: Sequelize.ENUM,
        values: ['employee','super employee','admin', 'super admin']
      },
      email: {
        allowNull: false,
        type: Sequelize.STRING
      },
      date_of_birth: {
        allowNull: false,
        type: Sequelize.DATEONLY
      },
      nationality: {
        allowNull: false,
        type: Sequelize.STRING
      },
      state_of_origin: {
        allowNull: false,
        type: Sequelize.STRING
      },
      phone_number: {
        allowNull: false,
        type: Sequelize.STRING
      },
      email: {
        allowNull: false,
        type: Sequelize.STRING
      },
      marital_status: {
        allowNull: false,
        type: Sequelize.STRING
      },
      home_address: {
        allowNull: false,
        type: Sequelize.STRING
      },
      id_type: {
        allowNull: false,
        type: Sequelize.STRING
      },
      id_number: {
        allowNull: false,
        type: Sequelize.STRING
      },
      id_expiry_date: {
        allowNull: false,
        type: Sequelize.DATEONLY
      },
      bank_name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      account_name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      account_number: {
        allowNull: false,
        type: Sequelize.STRING
      },
      bvn: {
        allowNull: false,
        type: Sequelize.STRING,
        unique: true
      },
      local_government_of_origin: {
        allowNull: false,
        type: Sequelize.STRING
      },
      religion: {
        allowNull: false,
        type: Sequelize.STRING
      },
      gender: {
        allowNull: false,
        type: Sequelize.STRING
      },
      blood_group: {
        allowNull: false,
        type: Sequelize.STRING,
      },
      disability: {
        allowNull: false,
        type: Sequelize.STRING
      },
      department: {
        allowNull: false,
        type: Sequelize.STRING
      },
      clearance_level: {
        allowNull: false,
        type: Sequelize.ENUM,
        values: ['employee', 'supervisor', 'admin', 'super admin']
      },
      date_of_employment: {
        allowNull: false,
        type: Sequelize.DATE
      },
      employment_type: {
        allowNull: false,
        type: Sequelize.ENUM,
        values: ['full time', 'contract']
      },
      date_of_contract_termination: {
        allowNull: false,
        type: Sequelize.DATE
      },
      form_of_id: {
        allowNull: false,
        type: Sequelize.STRING
      },
      payment_type: {
        allowNull: false,
        type: Sequelize.STRING,
      },
      salary_amount: {
        allowNull: false,
        type: Sequelize.DOUBLE,
      },
       spouse_name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      spouse_number: {
        allowNull: false,
        type: Sequelize.STRING
      },
      spouse_address: {
        allowNull: false,
        type: Sequelize.STRING
      },
      next_of_kin_name: {
        allowNull: false,
        type: Sequelize.STRING
      },
      next_of_kin_number: {
        allowNull: false,
        type: Sequelize.STRING
      },
      next_of_kin_address: {
        allowNull: false,
        type: Sequelize.STRING
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE, 
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Employees');
  }
};

See also, the CardRequest Model that belongs to Employee


'use strict';
module.exports = (sequelize, DataTypes) => {
  const IDCardRequest = sequelize.define('IDCardRequest', {
    card_request_status: DataTypes.ENUM('pending', 'processed'),
    card_renewal_status: DataTypes.ENUM('pending', 'processed')
  }, {
    underscored: true
  });

  IDCardRequest.associate = function(models) {
    // associations can be defined here
    IDCardRequest.BelongsTo(models.Employee,{
      foreignKey: 'employee_id',
      targetKey: 'employee_id',
      onDelete: 'CASCADE'
    })
  };
  IDCardRequest.removeAttribute('id');
  return IDCardRequest;
};
2
Can you show the full function code?404
Okay, I will edit as appropriateNayaya25

2 Answers

0
votes

Try to update ENUM declaration to this one:

role: {
        allowNull: false,
        type: Sequelize.ENUM('employee','super employee','admin', 'super admin'),
      }
0
votes

I solved this error by specifying the primary key on my model

employee_id: {
  type: DataTypes.INTEGER,
  primaryKey: true,
}

looks like it happens when you define associations but did not specify the primary key on your model.

Might be too late for the OP but hope this helps someone else.