Skip to content

fix: TypeDI do not inject service  #1241

@Mullayam

Description

@Mullayam

TypeDI was not able to inject sercie container in controller

Here is my code
i've tried all method written in documentation but still get error

image
image
image
image

Expected behavior

it should inject the dependencies and return json

Actual behavior

TypeError: this.test.getName is not a function
at BaseController.index (file:///E:/private/noidejs/nodets-pack/src/controllers/collection/base.controller.ts:14:28)
at ActionMetadata.callMethod (E:\private\noidejs\nodets-pack\node_modules.pnpm@enjoys+modules@1.1.1_class-transformer@0.5.1_class-validator@0.14.0\node_modules@enjoys\main\metadata\ActionMetadata.ts:252:44)
at E:\private\noidejs\nodets-pack\node_modules.pnpm@enjoys+modules@1.1.1_class-transformer@0.5.1_class-validator@0.14.0\node_modules@enjoys\main\RoutingControllers.ts:123:28
at processTicksAndRejections (node:internal/process/task_queues:95:5)

Activity

added
status: needs triageIssues which needs to be reproduced to be verified report.
type: fixIssues describing a broken feature.
on Sep 28, 2023
attilaorosz

attilaorosz commented on Sep 28, 2023

@attilaorosz
Member

Did you import reflect-metadata?

L-Chris

L-Chris commented on Aug 1, 2024

@L-Chris

same problem. It's ok in development mode, but crashed in production mode.

L-Chris

L-Chris commented on Aug 1, 2024

@L-Chris

my package.json
{ "name": "koa-ts", "version": "3.2.1", "license": "MIT", "description": "The best practice of building Koa2 with TypeScript", "main": "app.ts", "scripts": { "dev": "cross-env NODE_ENV=development; ts-node-dev -r tsconfig-paths/register app.ts", "dev:crawler": "cross-env NODE_ENV=development; ts-node-dev -r tsconfig-paths/register app/jobs/index.ts", "dev:import": "cross-env NODE_ENV=development; ts-node-dev -r tsconfig-paths/register app/jobs/import_library.ts", "dev:db": "docker compose -f docker-compose.yml up -d", "prettier": "prettier --write '**/*.{js,ts}'", "test": "jest --config .jest.config.js --no-cache --detectOpenHandles", "prod:build": "node ./build.js", "prod:start": "prisma generate && prisma migrate deploy && cross-env NODE_ENV=production; node ./dist/index.js", "prod:run": "npm run prod:build && cross-env NODE_ENV=production; node ./dist/index.js" }, "author": "unix (unix.bio@gmail.com)", "bugs": { "url": "https://github.com/unix/koa-ts/issues" }, "repository": "git@github.com:unix/koa-ts.git", "engines": { "node": ">= 14.x" }, "prettier": "@geist-ui/prettier-config", "devDependencies": { "@babel/plugin-proposal-export-namespace-from": "^7.18.9", "@babel/plugin-transform-modules-commonjs": "^7.24.8", "@babel/preset-typescript": "^7.24.7", "@geist-ui/prettier-config": "^1.0.1", "@types/crawler": "^1.2.6", "@types/fast-levenshtein": "^0.0.4", "@types/jest": "^25.2.2", "@types/koa": "^2.13.4", "@types/koa-bodyparser": "^4.3.5", "@types/koa-static": "^4.0.4", "@types/node": "^17.0.8", "@types/node-cron": "^3.0.11", "esbuild": "^0.14.11", "esbuild-node-externals": "^1.4.1", "jest": "^26.6.3", "prettier": "^2.5.1", "prisma": "^4.6.1", "supertest": "^4.0.2", "ts-jest": "^26.5.3", "ts-node": "10.9.2", "ts-node-dev": "2.0.0", "tsconfig-paths": "^3.12.0", "typescript": "^4.9.5" }, "dependencies": { "@koa/cors": "^5.0.0", "@prisma/client": "^4.6.1", "axios": "^1.7.2", "class-transformer": "0.5.1", "class-validator": "0.14.0", "crawler": "^1.5.0", "cross-env": "^7.0.3", "dayjs": "^1.11.12", "dotenv": "^12.0.3", "fast-levenshtein": "^3.0.0", "fix-esm": "^1.0.1", "koa": "^2.13.4", "koa-bodyparser": "^4.3.0", "koa-logger": "^3.2.1", "koa-multer": "^1.0.2", "koa-router": "^10.1.1", "koa-static": "^5.0.0", "node-cron": "^3.0.3", "queue": "^7.0.0", "reflect-metadata": "^0.1.13", "routing-controllers": "0.10.4", "typedi": "^0.10.0" } }

luisalbertodev

luisalbertodev commented on Aug 20, 2024

@luisalbertodev

You can see an example of hexagonal architecture with typedi for inject dependences

https://github.com/luisalbertodev/ddd-typescript-hexagonal-architecture

qinning001

qinning001 commented on Dec 22, 2024

@qinning001

You can use [InversifyJS](https://github.com/inversify/InversifyJS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @attilaorosz@L-Chris@luisalbertodev@qinning001@Mullayam

        Issue actions

          fix: TypeDI do not inject service · Issue #1241 · typestack/routing-controllers