Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providers doesn't exist in the current context #32

Open
tsaqifrazin1 opened this issue Feb 6, 2023 · 14 comments
Open

Providers doesn't exist in the current context #32

tsaqifrazin1 opened this issue Feb 6, 2023 · 14 comments

Comments

@tsaqifrazin1
Copy link

Screen Shot 2023-02-06 at 23 14 08

@alphamikle
Copy link
Owner

Hello @tsaqifrazin1, please, try the updated version 9.1.1

@tsaqifrazin1
Copy link
Author

Hi @alphamikle, Thanks for replying. I've updated the version but the error still exists

@tsaqifrazin1
Copy link
Author

Maybe this information help
this is my package.json

"dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/passport": "^9.0.1",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/swagger": "^6.1.2",
    "@nestjs/typeorm": "^9.0.1",
    "axios": "^1.3.2",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "compression": "^1.7.4",
    "dotenv": "^16.0.3",
    "dotenv-cli": "^7.0.0",
    "express-rate-limit": "^6.6.0",
    "helmet": "^6.0.1",
    "morgan": "^1.10.0",
    "nest-transact": "^9.1.1",
    "passport-jwt": "^4.0.1",
    "pg": "^8.8.0",
    "reflect-metadata": "^0.1.13",
    "request-context": "^2.0.0",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.9",
    "typeorm-transactional": "^0.4.1",
    "typeorm-transactional-cls-hooked": "^0.1.21"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.8",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.8",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },

this is my controller

@Controller('category')
@ApiTags('category')
@UseInterceptors(TransformResponseInterceptor)
export class CategoryController {
  constructor(
    private readonly categoryService: CategoryService,
    private readonly dataSource: DataSource,
  ) {}
  @Get()
  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesNameTypeGuard)
  @RolesNameType('kasiDalpersmil')
  async readAllCategories() {
    const category = await this.dataSource.transaction(async(manager) => {
      return  await this.categoryService.getCategories({});
    })
    return {
      message: 'Success read all category',
      data: category,
    };
  }
}

this is my service

@Injectable
export class CategoryService extends TransactionFor<CategoryService> {
  constructor(
    @InjectRepository(CategoryEntity)
    private readonly categoryRepository: Repository<CategoryEntity>,
    moduleRef: ModuleRef,
  ) {
    super(moduleRef);
  }
  ...
  ...
  ...
}

@alphamikle
Copy link
Owner

Hey @tsaqifrazin1! It seems that you did not use TransactionFor<T>` method .withTransaction. U just call

return  await this.categoryService.getCategories({});

instead of

return  await this.categoryService.withTransaction(manager).getCategories({});

@tsaqifrazin1
Copy link
Author

I'm sorry, I give you the wrong code.

@Controller('category')
@ApiTags('category')
@UseInterceptors(TransformResponseInterceptor)
export class CategoryController {
  constructor(
    private readonly categoryService: CategoryService,
    private readonly dataSource: DataSource,
  ) {}
  @Get()
  @ApiBearerAuth()
  @UseGuards(JwtAuthGuard, RolesNameTypeGuard)
  @RolesNameType('kasiDalpersmil')
  async readAllCategories() {
    const category = await this.dataSource.transaction(async(manager) => {
      return this.categoryService.withTransaction(manager).getCategories({});
    })
    return {
      message: 'Success read all category',
      data: category,
    };
  }
}

my code is like this

@devmarcellus
Copy link

Hi ,

The same error was reproduced on my side, while it worked perfectly after an npm install it no longer worked
I updated to the latest version but it still gives this error.

Error: Nest could not find Repository element (this provider does not exist in the current context)

@RazvanStetcu
Copy link

Same happens for me. Works fine locally but I got the same error on production environment

@mabalashov
Copy link

As a temporary solution. Downgrade packages nest/common and nest/core to version 9.2.1:

"@nestjs/common": "9.2.1",
"@nestjs/core": "9.2.1",

Works for me

@tsaqifrazin1
Copy link
Author

As a temporary solution. Downgrade packages nest/common and nest/core to version 9.2.1:

"@nestjs/common": "9.2.1",
"@nestjs/core": "9.2.1",

Works for me

Works for me too

@Fusix
Copy link

Fusix commented Mar 19, 2023

Hey this is a possible workaround but not a solution. With newer versions of NestJS we get the above error. Is there an updated solution on this topic?

@alphamikle
Copy link
Owner

Hey @Fusix. I have very limited resources at the moment and cannot deal with the problem. If you can solve this issue with a PR, I will accept it. That way you will help yourself and others.

@cccabdalla
Copy link

i am also here reporting the same issue which i am still struggling with !

Any reliable fix ?

@cccabdalla
Copy link

cccabdalla commented Apr 25, 2023

9.2.1

thanks, also worked for me but would prefer better fix to be done if possible.

for me, No downgrading but upgrading from "9.0.0" to "9.2.1" !

also wondering how on earth "@nestjs/common" and "@nestjs/core" had "9.0.0" version in the first place for project which was created 2 hours ago !

@maxpeterson
Copy link
Contributor

maxpeterson commented Feb 21, 2024

Having dug into this a little it seems that Nest is not finding the Repository dependencies anymore (dependency = this.moduleRef.get(param, { strict: false });). Using the repository token (string) seems to work so it is as if the Repository class no longer matches.

We have abandoned using nest_transact, but if someone has time I would suggest trying to create a failing NestJS example without using nest_transact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants