alphamikle / nest_transact

Simplest transactions support for Nestjs with Typeorm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Providers doesn't exist in the current context

tsaqifrazin1 opened this issue · comments

Hello @tsaqifrazin1, please, try the updated version 9.1.1

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

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);
  }
  ...
  ...
  ...
}

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({});

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

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)

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

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

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

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?

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.

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

Any reliable fix ?

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 !

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.