node.js - RepositoryNotFoundError:找不到“用户”的存储库。看起来这个实体没有在当前的“默认”连接中注册?打字机
问题描述
我在尝试让 TypeOrm 在我的 nestjs 项目中工作时遇到了一个有趣的问题。
我有下面的代码来配置我的项目,是的,一切都加载了,是的,我能够连接到我的数据库。
import { CacheModule, Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { ConfigModule } from '@nestjs/config';
import { AuthenticationController } from './controllers/authentication.controller';
import { AuthenticationService } from './services/authentication.service';
import { Connection } from 'typeorm';
import { BaseEntity } from './entities/base.entity';
@Module({
imports: [
ConfigModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: process.env.POSTGRE_PASSWORD,
database: process.env.DATABASE,
migrationsTableName: 'migration_table',
entities: [User, BaseEntity],
migrations: [__dirname + '/migrations/**/*.ts'],
subscribers: [__dirname + '/subscribers/**/*.ts'],
cli: {
entitiesDir: '/entitys',
migrationsDir: '/migrations',
subscribersDir: '/subscribers',
},
synchronize: true,
autoLoadEntities: true,
}),
CacheModule.register(),
PassportModule,
JwtModule.register({
secret: 'myprivatekey',
signOptions: { expiresIn: '1d' },
}),
],
controllers: [AuthenticationController],
providers: [AuthenticationService],
})
export class AppModule {
constructor(private connection: Connection) {}
}
以下是实体:
import {
Column,
BeforeUpdate,
BeforeInsert,
} from 'typeorm';
export class BaseEntity {
@Column()
created_at: Date;
@Column({
default: new Date(),
})
updated_at: Date;
@BeforeUpdate()
updateUpdatedAt() {
this.updated_at = new Date();
}
@BeforeInsert()
updateCreatedAt() {
this.created_at = new Date();
}
}
import {
Entity,
Column,
PrimaryGeneratedColumn,
Generated,
} from 'typeorm';
import { BaseEntity } from './base.entity';
@Entity('users')
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
@Generated('uuid')
uuid: string;
@Column()
first_name: string;
@Column()
last_name: string;
@Column()
email: string;
@Column()
password: string;
@Column({
default: false,
})
confirmed: boolean;
@Column({
default: null,
})
seller_id: string;
@Column({
default: null,
})
auth_token: string;
@Column({
default: false,
})
is_admin: boolean;
}
我最初尝试进行 glob 模式匹配,但无济于事,所以现在我直接在我的实体中导入,直到我可以运行一些东西。另请注意,我的所有模块都在上述错误之前加载,并且错误来自在 AuthenticationController 或 AdminController 中使用 @InjectRepository() 装饰器。我看过的所有地方都说这是因为我的实体没有被加载,我不确定这是怎么可能的。谢谢。
解决方案
在我的情况下,我在生产模式下遇到错误,为了修复它,我在文件夹中添加了编译的 JS 文件的路径build
。
const conn: MongoConnectionOptions = {
type: 'mongodb',
url: DB_URL,
synchronize: true,
useNewUrlParser: true,
useUnifiedTopology: true,
logging: true,
entities: ['src/entity/*.ts', './build/src/entity/*.js'], // <- Here!
migrations: ['src/migration/**/*.ts'],
subscribers: ['src/subscriber/**/*.ts'],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber',
},
extra: {
authSource: DB_AUTH_SOURCE,
},
};
简短的版本可能是:entities: ['**/src/entity/*{.ts,.js}'],
推荐阅读
- fpga - FPGA 软 IP 内核:它们通常总是依赖于芯片还是独立于芯片
- google-apps-script - 在不触发日历触发器的情况下创建或修改事件
- python - 在 Web_Scrapping 列表返回空列表
- java - 为什么我的链表的 listIterator.next().toString 方法不会返回有效的字符串?它抛出一个 java.util.ConcurrentModificationException
- python - PyQt5 QMainWindow 不显示中央小部件
- flutter - 单击时我将如何阻止构建按钮更改其状态谢谢
- javascript - xState 中的模型输入(文本字段)状态机
- python - 在python上实现一个永无止境的矩阵公式进行曲线拟合
- python - Python boto3分段上传视频到aws s3
- c - printf 没有给我正确的输出,也许 scanf 是罪魁祸首,请指教