gpt4 book ai didi

node.js - 均衡 : TypeError: Class constructor Modal cannot be invoked without 'new' (typescript)

转载 作者:行者123 更新时间:2023-12-03 22:36:18 25 4
gpt4 key购买 nike

我在 node 中使用 typescript,它在开发模式下运行良好,通过使用以下命令运行我的 index.ts 和 ts-node。

ts-node src/index.ts
但是当我将 typescript 代码转换为 es6 并使用简单的 node 命令运行 index.js 文件时
node dist/index.js
它抛出的错误是
/home/ubuntu/Projects/projects/Servers/test-server/node_modules/sequelize/lib/sequelize.js:486
this.importCache[importPath] = defineCall(this, DataTypes);
^
TypeError: Class constructor Modal cannot be invoked without 'new'
at Sequelize.import (/home/ubuntu/Projects/projects/Servers/test-server/node_modules/sequelize/lib/sequelize.js:486:38)
下面是我的 tsconfig.json 文件
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}
我尝试了与此错误相关的所有可用解决方案,但对我来说没有任何效果,但仍然出现此错误。
这是我的 Sequelize 索引文件。
import { Sequelize } from "sequelize";
import config from "../config";
import fs from "fs";
import path from "path"

export default class Modal {
public db: any = {};
private config = new config().config
constructor() {
this.registerSchemaInDB();
}
private sql: any = new Sequelize(
'mysql://' + this.config.database.username + ':' + this.config.database.password + '@localhost:3306/' + this.config.database.name, {
dialect: "mysql",
host: this.config.database.host
});
private registerSchemaInDB = () => {
fs.readdirSync(__dirname)
.filter((file) => {
return (file.indexOf(".") !== 0) && (file !== "index.ts") ;
})
.forEach((file) => {
console.log(file);
let model = this.sql.import(path.join(__dirname, file));
this.db[model.name] = model;
});

Object.keys(this.db).forEach((modelName) => {
if ("associate" in this.db[modelName]) {
this.db[modelName].associate(this.db);
}
});
this.db.sequelize = this.sql
}
}

Node version v12.14.0

Sequelize version 5.21.3


提前谢谢

最佳答案

当我尝试使用构建我的项目时,我遇到了同样的错误

tsc -p .



build 命令它工作正常完全正常。但在运行构建项目时抛出相同的错误

但我明白了我的错误。你只需要重构你的 fs.readdirSync 条件。

只需用下面的代码替换它
fs.readdirSync(__dirname)
.filter((file) => {
return (file.indexOf(".") !== 0) && (file !== "index.ts") && (file !== "index.js");
})
.forEach((file) => {
console.log(file);
let model = this.sql.import(path.join(__dirname, file));
this.db[model.name] = model;
});

你做错的是你只过滤 index.ts 文件你还必须检查 index,js

关于node.js - 均衡 : TypeError: Class constructor Modal cannot be invoked without 'new' (typescript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59999554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com