【问题标题】:How to Fix Sequelize TypeError: Cannot read property 'toUpperCase' of undefined如何修复 Sequelize TypeError:无法读取未定义的属性“toUpperCase”
【发布时间】:2019-12-08 17:14:27
【问题描述】:

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

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

我尝试更改其他 ENUM 字段的类型,如果它只有一个 ENUM 字段,它可以工作,但如果超过一个则不会工作。我不知道如何处理来自sequelize 库中的Sequelize camelize() 实用程序函数的类型错误。

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 (internal/modules/cjs/loader.js:689:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) 在 Module.load (internal/modules/cjs/loader.js:599:32) 在 tryModuleLoad (internal/modules/cjs/loader.js:538:12) 在 Function.Module._load (internal/modules/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 (internal/modules/cjs/loader.js:689:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) 在 Module.load (internal/modules/cjs/loader.js:599:32) 在 tryModuleLoad (internal/modules/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');
  }
};

另见,属于 Employee 的 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;
};

【问题讨论】:

  • 能展示完整的功能代码吗?
  • 好的,我会酌情修改

标签: node.js postgresql sequelize.js


【解决方案1】:

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

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

当您定义关联但未在模型上指定主键时,似乎会发生这种情况。

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

【讨论】:

    【解决方案2】:

    尝试将 ENUM 声明更新为此:

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

    【讨论】:

    • 我已经这样做了。如果我使用该语法,它将不允许多个枚举类型定义。目前错误来自我的模型文件夹的 index.js 文件。
    猜你喜欢
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2015-09-08
    • 2021-10-03
    • 2021-10-28
    相关资源
    最近更新 更多