NodeJS TypeORM数据源配置返回未定义

5gfr0r5j  于 2023-06-05  发布在  Node.js
关注(0)|答案(1)|浏览(153)

我正在用node和express写一个微服务应用。我在mysql中使用typeorm进行关系数据库Map。我已经完全遵循了文档,并按照文档中所说的配置了所有内容。但是现在,当我尝试调试时,数据源被称为未定义。这是我第一次使用typeorm库。感谢任何人能帮助我解决这个问题。谢谢

数据源.ts

import "reflect-metadata";
import {DataSource} from 'typeorm';

/**
 * Various data source can be configured here
 */
export const MySQLDataSource = new DataSource({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "password",
    database: "ignite_tuition_node_db",
    entities: ["dist/entities/*.js"],
    logging: true,
    synchronize: true,
    entitySkipConstructor: true,
});

index.ts

import "reflect-metadata";
import express, { Application } from "express";
const expressApp = require('./express-app');

const StartServer = async() => {

    // initialize the application server
    const app: Application = express();
    const PORT = process.env.PORT || 8000;
    
    await expressApp(app);

    // listen to the application server on PORT
    app.listen(PORT, (): void => {
        console.log(`Express application server running on address=> http://localhost:${PORT}`);
    });
}

StartServer();

express-app.ts

/**
 * Express app
 */
module.exports = async (app: Application) => {

    // initialize mysql datasource
    MySQLDataSource.initialize()
    .then(() => console.log("MySQL Datasource has been successfully initialized"))
    .catch((e) => console.error("Error initializing mysql datasource", e));
    
    // configuring necessary properties for server
    app.use(express.json());

    // controller configuration
    TuitionController(app);
}

仓库.ts

export function getRepository(entity: any): Repository<any> {
    try {
        return MySQLDataSource.getRepository(entity);
    } catch (e) {
        throw new TuitionServiceException(`Error occurred when finding ${entity} repository`);
    }
}

当我在调试器控制台的Repository.ts中计算MySQLDataSource时,我收到

MySQLDataSource
Uncaught ReferenceError ReferenceError: MySQLDataSource is not defined
    at eval (repl:1:1)
chhqkbe1

chhqkbe11#

import "reflect-metadata";
import express, { Application } from "express";
const expressApp = require('./express-app');
const MySQLDataSource = require('./MySQLDataSource');

const StartServer = async() => {

    // initialize the application server
    const app: Application = express();
    const PORT = process.env.PORT || 8000;
    
    await expressApp(app);

    await MySQLDataSource.initialize();

    // listen to the application server on PORT
    app.listen(PORT, (): void => {
        console.log(`Express application server running on address=> http://localhost:${PORT}`);
    });
}

StartServer();

仓库.ts

import MySQLDataSource from './MySQLDataSource'

await MySQLDataSource.getRepository(Entity)

数据源

import "reflect-metadata";
import {DataSource} from 'typeorm';

/**
 * Various data source can be configured here
 */
const MySQLDataSource = new DataSource({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "password",
    database: "ignite_tuition_node_db",
    entities: [__dirname + '/./entities/**/*.{js,ts}'],
    migrations: [path.join(__dirname, './migrations/*')]
    logging: false,
    synchronize: true,
    entitySkipConstructor: true,
});

export default MySQLDataSource;

相关问题