简体   繁体   中英

TypeError: defineCall is not a function. require() fails

I've recently joined a project where a database model is built with Sequelize. However, I can't import more than a few files with sequelize.import before getting TypeError: defineCall is not a function . There seems to be errors using require() for importing the default function of certain files. I have the following structure:

models/index.ts

import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';

const sequelize = new Sequelize('mysql://root:password@localhost/myDatabase', {
    dialect: 'mysql',
    logging: false
});

const db: any = {};

fs.readdirSync(__dirname).filter(file => {
    return (file.indexOf('.') !== 0) && (file !== 'index.ts') && (file.endsWith('.js'));
}).forEach(file => {
    const model = sequelize.import(path.join(__dirname, file)); //<<<<<<<<<< This brings an error
    db[model.name.charAt(0).toUpperCase() + model.name.slice(1)] = model;
});

and where it fails:

sequelize.js

  import(path) {
    // is it a relative path?
    if (Path.normalize(path) !== Path.resolve(path)) {
      // make path relative to the caller
      const callerFilename = Utils.stack()[1].getFileName();
      const callerPath = Path.dirname(callerFilename);
      path = Path.resolve(callerPath, path);
    }

    if (!this.importCache[path]) {
      let defineCall = arguments.length > 1 ? arguments[1] : require(path);

      if (typeof defineCall === 'object') {
        // ES6 module compatability
        defineCall = defineCall.default;
      }

      this.importCache[path] = defineCall(this, DataTypes);// <<<<<<<< defineCall is undefined here
    }

    return this.importCache[path];
  }

A typical file which results in an undefined defineCall:

models/action-calls.js

export default function(sequelize, DataTypes) {
    return sequelize.define('actionCalls', {
        id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
        actionId: { type: DataTypes.INTEGER, allowNull: false },
        userId: { type: DataTypes.INTEGER, allowNull: false },
        date: { type: DataTypes.DATE, allowNull: false },
        number: { type: DataTypes.STRING(32), allowNull: false },
        duration: { type: DataTypes.INTEGER }
    }, {
        tableName: 'ActionCalls',
        timestamps: false
    });
}

Oddly enough, sometimes this file gets imported without issues but then another file eventually gets the error. I've looked through all the files in the models/ folder, and they all follow the same pattern as action-calls.js . I had no problems with this until last week when it first appeared. My colleague has not experienced the error with the same codebase. I've tried going back to an earlier commit where it used to work but the error now stays. Any help would be appreciated.

It turned out the problem was with ts-node-dev. After updating it from 1.0.0-pre.49 to 1.1.6 the error disappeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM