我正在用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)
1条答案
按热度按时间chhqkbe11#
仓库.ts
数据源