javascript - 类型错误: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相同的模式。直到上周它首次出现时,我才对此没有任何问题。我的同事没有遇到相同代码库的错误。我尝试回到以前可以工作的较早提交,但现在错误仍然存在。任何帮助,将不胜感激。
解决方案
原来问题出在 ts-node-dev 上。从 1.0.0-pre.49 更新到 1.1.6 后,错误消失了。
推荐阅读
- kotlin - 将字符串拆分成对
- django - 带有多个应用程序的 Django + Vue
- c - 当我在 C 中比较完全相同的字符串时,strcmp() 返回 10
- python - Python - 如何扩展 Pandas 数据框的行以包含键列值的所有组合?
- python - Django 向导表单和其他视图在一页中
- visual-studio - 是否可以从不同的机器上用 Visual Studio 打开一个项目?
- playframework-2.6 - play-mailer 是 smtp 服务器吗
- shell - HBase:COLUMN 和 FILTER (SingleColumnValueFilter) 之间的混淆
- php - Magento v1.9.x 订阅垃圾邮件
- kubernetes - Kubernetes 上的 Apache flink - 如果 jobmanager 崩溃则恢复工作