如何修复Sequelize TypeError:无法读取未定义的属性“ toUpperCase”

时间:2019-07-31 08:30:14

标签: node.js postgresql sequelize.js

迁移模型后,我将启动节点开发服务器。我怎么会遇到两个错误:

  

在我的模型中声明多个ENUM数据类型时出现TypeError和And Error。

我尝试更改其他ENUM字段的类型,如果只有一个ENUM字段,则它起作用,但如果有多个,则不起作用。我不知道如何处理Sequelize camelize()库中sequelize实用程序功能的类型错误。

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

TypeError:无法读取未定义的属性“ toUpperCase”       在str.trim.replace(A:\ Portal_project \ customer-portal \ Server \ node_modules \ sequelize \ lib \ utils.js:94:61)       在String.replace()       在Object.camelize(A:\ Portal_project \ customer-portal \ Server \ node_modules \ sequelize \ lib \ utils.js:94:21)       在新的HasOne(A:\ Portal_project \ customer-portal \ Server \ node_modules \ sequelize \ lib \ associations \ has-one.js:43:31)       在功能。 (A:\ Portal_project \ customer-portal \ Server \ node_modules \ sequelize \ lib \ associations \ mixin.js:105:25)       在Function.Employee.associate(A:\ Portal_project \ customer-portal \ Server \ db \ models \ employee.js:58:14)       在Object.keys.forEach.modelName(A:\ Portal_project \ customer-portal \ Server \ db \ models \ index.js:30:19)       在Array.forEach()       在对象。 (A:\ Portal_project \ customer-portal \ Server \ db \ models \ index.js:28:17)       在Module._compile(内部/模块/cjs/loader.js:689:30)       在Object.Module._extensions..js(内部/模块/cjs/loader.js:700:10)       在Module.load(internal / modules / cjs / loader.js:599:32)       在tryModuleLoad(内部/模块/cjs/loader.js:538:12)       在Function.Module._load(内部/模块/cjs/loader.js:530:3)       在Module.require(internal / modules / cjs / loader.js:637:17)       在要求时(内部/模块/cjs/helpers.js:22:18)       在对象。 (A:\ Portal_project \ customer-portal \ Server \ api \ controllers \ customerController.js:1:78)       在Module._compile(内部/模块/cjs/loader.js:689:30)       在Object.Module._extensions..js(内部/模块/cjs/loader.js:700:10)       在Module.load(internal / modules / cjs / loader.js:599:32)       在tryModuleLoad(内部/模块/cjs/loader.js:538:12)       在Function.Module._load(internal / modules / cjs / loader.js:530:3)

我希望所有sequelize迁移都能正常工作,并且我的开发服务器也将启动。但我一直收到此类型错误。

这是我的员工模型

'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;
};


和迁移文件

'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');
  }
};

另请参阅属于员工的CardRequest模型


'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 个答案:

答案 0 :(得分:0)

尝试将ENUM声明更新为此:

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

答案 1 :(得分:0)

我通过在模型上指定主键解决了该错误

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

看起来好像是在定义关联但未在模型上指定主键时发生的。

对于OP来说可能为时已晚,但希望这对其他人有帮助。