I've encountered a challenge while working on an API using nestjs, specifically with service injection in a guard. The error message I'm facing is:
Error: Nest can't resolve dependencies of the AuthorizerGuard (?). Please make sure that the argument Object at index [0] is available in the PostsModule context.
I've searched through various posts related to this issue but none of the solutions seem to work for me as I'm relatively new to nestjs and struggling to understand what needs to be done.
Here are my current files:
authorizer.module.ts
@Module({
providers: [AuthorizerService],
exports: [AuthorizerService],
})
export class AuthorizerModule {}
authorizer.guard.ts
import {
CanActivate,
ExecutionContext,
Inject,
Injectable,
Logger,
UnauthorizedException,
} from '@nestjs/common';
@Injectable()
export class AuthorizerGuard implements CanActivate {
constructor(private readonly AuthorizerService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
//my code
}
}
posts.module.ts
@Module({
imports: [AuthorizerModule],
controllers: [PostsController],
providers: [PostsService],
})
export class PostsModule {}
posts.controller.ts
@UseGuards(AuthorizerGuard)
export class PostsController {}
authorizer.service.ts
@Injectable()
export class AuthorizerService {
prisma = new PrismaClient();
async findUseridByEmail(email: string) {
return await this.prisma.user.findUnique({
where: {
email,
},
});
}
}
Appreciate any assistance you can provide. Thank you!