#Repository method is not a function?

1 messages · Page 1 of 1 (latest)

ember hamlet
#

auth.module.ts

import { TypeOrmModule } from '@nestjs/typeorm';

const repositories = [ApiKeysRepository];

const services = [AuthService];

@Module({
  imports: [TypeOrmModule.forFeature([...repositories])],
  providers: [...services],
  exports: [...services],
})
export class AuthModule {}

auth.service.ts

import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(ApiKeysRepository)
    private apiKeysRepository: ApiKeysRepository
  ) {}

  async findByApiKey(apiKey: string): Promise<ApiKeys> {
    const apiKeys: ApiKeys = await this.apiKeysRepository.findByApiKey(
      apiKey,
    );

    return apiKeys;
  }
}

apiKeys.repository.ts

import { EntityRepository, Repository } from 'typeorm';
import { ApiKeys } from '../entities/ApiKeys.entity';

@EntityRepository(ApiKeys)
export class ApiKeysRepository extends Repository<ApiKeys> {

  async findByApiKey(apiKey: string): Promise<ApiKeys> {
    const query = await this.findOne({
      where: {
        Key: apiKey,
      },
    });

    return query;
  }

}

package.json

{
    "@nestjs/axios": "^2.0.0",
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/typeorm": "^9.0.1",
    "typeorm": "^0.3.13",
    "mssql": "^9.1.1",
}
wanton swan
hushed cove
#

You're missing the @Injectable() decorator on your AuthService.

stone nexus
ember hamlet
# stone nexus Technically it's not needed because of the `@InjectRepository()` inside the cons...

I used this workaround instead and it works. Any thoughts on it?

https://gist.github.com/anchan828/9e569f076e7bc18daf21c652f7c3d012

Gist

This is an improvement to allow @nestjs/[email protected] to handle CustomRepository. I won't explain it specifically, but it will help in some way. https://github.com/nestjs/typeorm/pull/1233 ...