TypeError: Cannot read properties of undefined on NestJS Dependency Injection
TypeError: Cannot read properties of undefined on NestJS Dependency Injection
我正在 TypeError: Cannot read properties of undefined (reading 'create') at AuthenticationService.register
并且花了很多时间阅读这个网站(和其他网站)试图弄清楚我错过了什么。可能涉及到 Typeorm 是我知道自己无法匹敌的地方——任何帮助都将不胜感激。
users.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { UsersService } from './users.service';
@Module({
imports: [TypeOrmModule.forFeature([User])],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
users.service.ts
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './user.entity';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
async create(userData: CreateUserDto) {
const newUser = await this.usersRepository.create(userData);
await this.usersRepository.save(newUser);
return newUser;
}
}
authentication.module.ts
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { AuthenticationController } from './authentication.controller';
import { AuthenticationService } from './authentication.service';
import { UsersModule } from '../users/users.module';
import { LocalStrategy } from './local.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
UsersModule,
PassportModule,
ConfigModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secret: configService.get('JWT_SECRET'),
signOptions: {
expiresIn: `${configService.get('JWT_EXPIRATION_TIME')}`,
},
}),
}),
],
providers: [AuthenticationService, LocalStrategy, JwtStrategy],
controllers: [AuthenticationController],
exports: [AuthenticationService],
})
export class AuthenticationModule {}
authentication.service.ts
import { HttpException, HttpStatus } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcrypt';
import { RegisterDto } from './dto/register.dto';
import { TokenPayload } from './token-payload.interface';
import { PostgresErrorCode } from '../database/postgres-error-codes.enum';
import { UsersService } from 'src/users/users.service';
export class AuthenticationService {
constructor(
private readonly configService: ConfigService,
private readonly jwtService: JwtService,
private readonly usersService: UsersService,
) {}
public async register(registrationData: RegisterDto) {
const hashedPassword = await bcrypt.hash(registrationData.password, 10);
try {
const createdUser = await this.usersService.create({
...registrationData,
password: hashedPassword,
});
createdUser.password = undefined;
return createdUser;
} catch (error) {
if (error?.code === PostgresErrorCode.UniqueViolation) {
throw new HttpException(
'User with that email already exists',
HttpStatus.BAD_REQUEST,
);
}
console.log(`error `, error);
throw new HttpException(
'Something went wrong',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
authentication.controller.ts
import {
Body,
Req,
Controller,
HttpCode,
Post,
UseGuards,
Res,
Get,
} from '@nestjs/common';
import { AuthenticationService } from './authentication.service';
import { RegisterDto } from './dto/register.dto';
import { UsersService } from 'src/users/users.service';
@Controller('authentication')
export class AuthenticationController {
constructor(
private readonly authenticationService: AuthenticationService,
private readonly usersService: UsersService,
) {}
@Post('register')
async register(@Body() registrationData: RegisterDto) {
console.log(`registrationData: `, registrationData);
// return this.usersService.create(registrationData); // This works
return this.authenticationService.register(registrationData); // This throws error
}
}
将 @Injectable()
添加到您的 AuthenticationService
class。
我正在 TypeError: Cannot read properties of undefined (reading 'create') at AuthenticationService.register
并且花了很多时间阅读这个网站(和其他网站)试图弄清楚我错过了什么。可能涉及到 Typeorm 是我知道自己无法匹敌的地方——任何帮助都将不胜感激。
users.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { UsersService } from './users.service';
@Module({
imports: [TypeOrmModule.forFeature([User])],
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}
users.service.ts
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateUserDto } from './dto/create-user.dto';
import { User } from './user.entity';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
async create(userData: CreateUserDto) {
const newUser = await this.usersRepository.create(userData);
await this.usersRepository.save(newUser);
return newUser;
}
}
authentication.module.ts
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { AuthenticationController } from './authentication.controller';
import { AuthenticationService } from './authentication.service';
import { UsersModule } from '../users/users.module';
import { LocalStrategy } from './local.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
UsersModule,
PassportModule,
ConfigModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secret: configService.get('JWT_SECRET'),
signOptions: {
expiresIn: `${configService.get('JWT_EXPIRATION_TIME')}`,
},
}),
}),
],
providers: [AuthenticationService, LocalStrategy, JwtStrategy],
controllers: [AuthenticationController],
exports: [AuthenticationService],
})
export class AuthenticationModule {}
authentication.service.ts
import { HttpException, HttpStatus } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcrypt';
import { RegisterDto } from './dto/register.dto';
import { TokenPayload } from './token-payload.interface';
import { PostgresErrorCode } from '../database/postgres-error-codes.enum';
import { UsersService } from 'src/users/users.service';
export class AuthenticationService {
constructor(
private readonly configService: ConfigService,
private readonly jwtService: JwtService,
private readonly usersService: UsersService,
) {}
public async register(registrationData: RegisterDto) {
const hashedPassword = await bcrypt.hash(registrationData.password, 10);
try {
const createdUser = await this.usersService.create({
...registrationData,
password: hashedPassword,
});
createdUser.password = undefined;
return createdUser;
} catch (error) {
if (error?.code === PostgresErrorCode.UniqueViolation) {
throw new HttpException(
'User with that email already exists',
HttpStatus.BAD_REQUEST,
);
}
console.log(`error `, error);
throw new HttpException(
'Something went wrong',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
authentication.controller.ts
import {
Body,
Req,
Controller,
HttpCode,
Post,
UseGuards,
Res,
Get,
} from '@nestjs/common';
import { AuthenticationService } from './authentication.service';
import { RegisterDto } from './dto/register.dto';
import { UsersService } from 'src/users/users.service';
@Controller('authentication')
export class AuthenticationController {
constructor(
private readonly authenticationService: AuthenticationService,
private readonly usersService: UsersService,
) {}
@Post('register')
async register(@Body() registrationData: RegisterDto) {
console.log(`registrationData: `, registrationData);
// return this.usersService.create(registrationData); // This works
return this.authenticationService.register(registrationData); // This throws error
}
}
将 @Injectable()
添加到您的 AuthenticationService
class。