I am trying to use TypeORM inside a class but for some reason it can not find the default connection, I am awaiting the connection and I am sure that the config is right because I tested it with .then() and that did work
class App {
public app: express.Application;
constructor() {
this.app = express();
this.connect();
this.test();
this.config();
this.routes();
}
private async connect(): Promise<Connection> {
return createConnection();
}
private async test(): Promise<User> {
const repository = getRepository(User);
const user = new User();
user.firstName = 'Daniell';
user.lastName = 'lastname';
return repository.save(user);
}
I call the class like
import App from './App';
import { Server } from './Server';
(() => new Server(App))();
Why can it not find the default connection?
Fixed it by changing the class like
class App {
public app: express.Application;
private connection: Promise<Connection>;
constructor() {
this.connection = createConnection();
this.config();
}
private async config(): Promise<void> {
// Load TypeORM with ormconfig.json options
await this.connection;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.