【发布时间】:2018-09-22 11:08:42
【问题描述】:
我将 TypeORM 与 NestJS 一起使用,但无法正确保存实体。
连接创建成功,postgres 在 5432 端口上运行。凭证也可以。
但是,当我需要使用 entity.save() 保存资源时,我得到了:
Connection "default" was not found.
Error
at new ConnectionNotFoundError (/.../ConnectionNotFoundError.ts:11:22)
我检查了 TypeORM ConnectionManager (https://github.com/typeorm/typeorm/blob/master/src/connection/ConnectionManager.ts) 的源文件,但似乎 TypeORM 第一次创建连接时,如果我们不提供连接,它的属性是“默认”名称,对我来说就是这种情况。
我将 TypeORM 设置为 TypeOrmModule
TypeOrmModule.forRoot({
type: config.db.type,
host: config.db.host,
port: config.db.port,
username: config.db.user,
password: config.db.password,
database: config.db.database,
entities: [
__dirname + '/../../dtos/entities/*.entity.js',
]
})
当然我的常数是正确的。有什么想法吗?
【问题讨论】:
-
我假设您必须将
entities提供给 TypeORM 配置(例如entities: 'src/*/**.entity.ts')。 -
是的,我以这种方式引用了我的实体:
entities: [ __dirname + '/../../dtos/entities/*.entity.js', ],我编辑了我的帖子
标签: node.js postgresql typeorm nestjs