首页 > 解决方案 > 类型错误:defineCall 不是函数。要求()失败

问题描述

我最近加入了一个使用 Sequelize 构建数据库模型的项目。但是,在获取.sequelize.import之前,我不能导入多个文件TypeError: defineCall is not a function使用require()导入某些文件的默认功能似乎有错误。我有以下结构:

模型/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;
});

以及失败的地方:

续集.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];
  }

导致未定义 defineCall 的典型文件:

模型/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
    });
}

奇怪的是,有时这个文件被导入没有问题,但另一个文件最终得到错误。我查看了 models/ 文件夹中的所有文件,它们都遵循与action-calls.js相同的模式。直到上周它首次出现时,我才对此没有任何问题。我的同事没有遇到相同代码库的错误。我尝试回到以前可以工作的较早提交,但现在错误仍然存​​在。任何帮助,将不胜感激。

标签: javascriptnode.jssequelize.js

解决方案


原来问题出在 ts-node-dev 上。从 1.0.0-pre.49 更新到 1.1.6 后,错误消失了。


推荐阅读