javascript - 类型错误 : defineCall is not a function. require() 失败

标签 javascript node.js sequelize.js

我最近加入了一个使用 Sequelize 构建数据库模型的项目。但是,在获取 TypeError: defineCall is not a function 之前,我无法使用 sequelize.import 导入多个文件。使用 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;
});
以及失败的地方:
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];
  }
导致未定义的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 相同的模式。直到上周它第一次出现时,我才遇到这个问题。我的同事没有遇到相同代码库的错误。我试过回到以前的提交,它曾经可以工作,但现在错误仍然存​​在。任何帮助,将不胜感激。

最佳答案

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

关于javascript - 类型错误 : defineCall is not a function. require() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67062082/

相关文章:

javascript - Twitter Bootstrap 导航栏选项卡 "hide"事件?

javascript - 运行 JavaScript 模块返回 SyntaxError : Unexpected token export

mysql - Sequelize 中带有联接表的配偶关系

javascript - 调用 Node.js 中具有 Promise 的函数

javascript - 无法从ajax获取post数据到php

javascript - 检查文本字段是否为空或不是jquery

javascript - 如何获取我在 Jquery Tabs UI 中命名的当前选项卡标题

javascript - "Promise fires on the same turn of the event loop"是什么意思?

node.js - 即使添加到环境 PATH,命令提示符也无法识别 MongoDB

sql - 联接是进行跨表查询的正确方法吗?