NestJS y TypeORM:内置存储库未定义
NestJS y TypeORM: built in Repository undefined
我做了 Inject Repository(User) 但它对我不起作用。
我想打电话给:this.users Repository.create
但是给出这个错误:
类型错误:this.users Repository.create 不是函数
......
我做了 Inject Repository(User) 但它对我不起作用。
我想打电话给:this.users Repository.create
但是给出这个错误:
类型错误:this.users Repository.create 不是函数
服务:
import { HttpException, HttpStatus, Inject, Injectable,forwardRef } from '@nestjs/common';
import { AuthenticationService } from 'src/authentication/authentication.service';
import { Repository } from 'typeorm';
import CreateUserDto from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import User from './entities/user.entity';
@Injectable()
export class UserService {
constructor(
@Inject(forwardRef(() => AuthenticationService))
// @Inject(User)
// private usersRepository: Repository<User>
@InjectRepository(User) private usersRepository: Repository<User>,
private readonly authenticationService: AuthenticationService,
) {}
async getByEmail(email: string) {
const user = await this.usersRepository.findOne({ email });
if (user) {
return user;
}
throw new HttpException('User with this email does not exist', HttpStatus.NOT_FOUND);
}
async getById(id: number) {
const user = await this.usersRepository.findOne({ id });
if (user) {
return user;
}
throw new HttpException('User with this id does not exist', HttpStatus.NOT_FOUND);
}
async create(userData: CreateUserDto) {
const newUser = await this.usersRepository.create(userData);
await this.usersRepository.save(newUser);
return newUser;
}
}
模块:
import { Module,forwardRef } from '@nestjs/common';
import { UserService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import User from './entities/user.entity';
import { UsersController } from './users.controller';
import { AuthenticationService } from 'src/authentication/authentication.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule, JwtService } from '@nestjs/jwt';
@Module({
imports: [TypeOrmModule.forFeature([User]),JwtModule.register({})],
providers: [UserService,AuthenticationService,ConfigService],
exports: [UserService,AuthenticationService,ConfigService],
controllers:[UsersController]
})
export class UsersModule {}
实体:
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('User')
class User {
@PrimaryGeneratedColumn()
public id?: number;
@Column({ unique: true })
public email: string;
@Column()
public name: string;
@Column()
public password: string;
}
export default User;
我找到了一个解决方案,方法是创建自定义存储库并使用内置功能对其进行扩展,效果很好。
这是link:
https://clownhacker.tistory.com/250
我认为它发生是因为我对我的实体进行了更改 class,我希望它能对某人有所帮助。
我做了 Inject Repository(User) 但它对我不起作用。
我想打电话给:this.users Repository.create
但是给出这个错误: 类型错误:this.users Repository.create 不是函数
......
我做了 Inject Repository(User) 但它对我不起作用。
我想打电话给:this.users Repository.create
但是给出这个错误: 类型错误:this.users Repository.create 不是函数
服务:
import { HttpException, HttpStatus, Inject, Injectable,forwardRef } from '@nestjs/common';
import { AuthenticationService } from 'src/authentication/authentication.service';
import { Repository } from 'typeorm';
import CreateUserDto from './dto/create-user.dto';
import { InjectRepository } from '@nestjs/typeorm';
import User from './entities/user.entity';
@Injectable()
export class UserService {
constructor(
@Inject(forwardRef(() => AuthenticationService))
// @Inject(User)
// private usersRepository: Repository<User>
@InjectRepository(User) private usersRepository: Repository<User>,
private readonly authenticationService: AuthenticationService,
) {}
async getByEmail(email: string) {
const user = await this.usersRepository.findOne({ email });
if (user) {
return user;
}
throw new HttpException('User with this email does not exist', HttpStatus.NOT_FOUND);
}
async getById(id: number) {
const user = await this.usersRepository.findOne({ id });
if (user) {
return user;
}
throw new HttpException('User with this id does not exist', HttpStatus.NOT_FOUND);
}
async create(userData: CreateUserDto) {
const newUser = await this.usersRepository.create(userData);
await this.usersRepository.save(newUser);
return newUser;
}
}
模块:
import { Module,forwardRef } from '@nestjs/common';
import { UserService } from './users.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import User from './entities/user.entity';
import { UsersController } from './users.controller';
import { AuthenticationService } from 'src/authentication/authentication.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule, JwtService } from '@nestjs/jwt';
@Module({
imports: [TypeOrmModule.forFeature([User]),JwtModule.register({})],
providers: [UserService,AuthenticationService,ConfigService],
exports: [UserService,AuthenticationService,ConfigService],
controllers:[UsersController]
})
export class UsersModule {}
实体:
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('User')
class User {
@PrimaryGeneratedColumn()
public id?: number;
@Column({ unique: true })
public email: string;
@Column()
public name: string;
@Column()
public password: string;
}
export default User;
我找到了一个解决方案,方法是创建自定义存储库并使用内置功能对其进行扩展,效果很好。
这是link: https://clownhacker.tistory.com/250
我认为它发生是因为我对我的实体进行了更改 class,我希望它能对某人有所帮助。