question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't inject services which extend RepositoryService

See original GitHub issue

I tried to implement a custom validator with class-validator https://github.com/typestack/class-validator#custom-validation-classes. I used the example project from this repository.

I added the following class:

import { ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
import { Injectable } from '@nestjs/common';
import { UsersService } from './users.service';

@ValidatorConstraint({ name: 'doesUserAlreadyExist', async: true })
@Injectable()
export class DoesUserAlreadyExist implements ValidatorConstraintInterface {
  constructor(private usersService: UsersService) {}

  async validate(text: string) {
    const users = await this.usersService.getMany({
        filter: [
            {
                field: 'email',
                operator: 'eq',
                value: text
            }
        ]
    });
    return users.length === 0 ? true : false;
  }
}

And added the validator to the User class (Entity):

...
  @Validate(DoesUserAlreadyExist, {
    message: 'User already exists',
  })
  @Column({ type: 'varchar', length: 255, nullable: false, unique: true })
  email: string;
...

I get the following error:

/Users/kai/Development/NodeJS/crud/integration/typeorm/node_modules/@nestjs/typeorm/dist/common/typeorm.utils.js:8
    return `${entity.name}Repository`;
                     ^
TypeError: Cannot read property 'name' of undefined
    at Object.getRepositoryToken (/Users/kai/Development/NodeJS/crud/integration/typeorm/node_modules/@nestjs/typeorm/dist/common/typeorm.utils.js:8:22)
    at Object.exports.InjectRepository (/Users/kai/Development/NodeJS/crud/integration/typeorm/node_modules/@nestjs/typeorm/dist/common/typeorm.decorators.js:5:72)
    at Object.<anonymous> (/Users/kai/Development/NodeJS/crud/integration/typeorm/src/users/users.service.ts:12:16)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Module.m._compile (/Users/kai/Development/NodeJS/crud/integration/typeorm/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/kai/Development/NodeJS/crud/integration/typeorm/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)

If I remove the injected UsersService dependency injection from the DoesUserAlreadyExist class everything works again. Is it not possible to use the services which extends the RepositoryService service? Or is there another way to execute the repository methods (getMany, getOne, etc.)?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
kairauercommented, Mar 15, 2019

To anyone else who gets a similar issue, I think this is related to this issue: https://github.com/nestjs/nest/issues/528

1reaction
michaelyalicommented, Mar 11, 2019

Yes, that was my point. And, in general, I think it has nothing to do with RepositoryService or with NestJs, but I think it’s a TypeORM issue instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you inject a service in NestJS into a typeorm repository?
Short answer: you don't. TypeORM's custom repositories, repository classes, and entities are technically outside of Nest's DI system, ...
Read more >
How to use abstracted repositories in dependency injection
Subordinate services are injected to the client class. The client class cannot have explicit dependencies on subordinate elements; ...
Read more >
Inject a repository instead of an entity manager
It instructs the service container to call the getRepository() method on the entity manager service with the entity class name as the first ......
Read more >
@Component vs @Repository and @Service in Spring ...
In this quick tutorial, we're going to learn about the differences between the @Component, @Repository, and @Service annotations in the Spring Framework.
Read more >
How to use Repository with Doctrine as Service in Symfony
Dependency injection with autowiring is super easy [since Symfony ... We cannot inject repository to other service just via constructor ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found