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

Keep getting "Cannot resolve all parameters for..." error #16

Closed
galtalmor opened this issue Jul 28, 2017 · 6 comments
Closed

Keep getting "Cannot resolve all parameters for..." error #16

galtalmor opened this issue Jul 28, 2017 · 6 comments

Comments

@galtalmor
Copy link
Contributor

I'm trying to use this in a node project written in TypeScript (tried running it with ts-node and with the code compiled to JS).
I've got to the point where I just copied the very basic example from the readme, and still get the same result.
I made sure 'reflect-metadata' and 'injection-js' both installed and on latest version.
What am I missing?

This is the code:

import 'reflect-metadata';
import { ReflectiveInjector, Injectable, Injector } from 'injection-js';

class Http {}

@Injectable()
class Service {
    constructor(private http: Http) {}
}

const injector = ReflectiveInjector.resolveAndCreate([
    Service,
    Http
]);

console.log(injector.get(Service) instanceof Service);

And this is the exception:

D:\proj>ts-node "./src/index.ts"

D:\proj\node_modules\injection-js\injection.bundle.js:684
    return Error('Cannot resolve all parameters for \'' + stringify(typeOrFunc) + '\'(' +
           ^
Error: Cannot resolve all parameters for 'Service'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Service' is decorated with Injectable.
    at noAnnotationError (D:\proj\node_modules\injection-js\injection.bundle.js:684:12)
    at _dependenciesFor (D:\proj\node_modules\injection-js\injection.bundle.js:1377:15)
    at resolveReflectiveFactory (D:\proj\node_modules\injection-js\injection.bundle.js:1277:24)
    at resolveReflectiveProvider (D:\proj\node_modules\injection-js\injection.bundle.js:1300:82)
    at Array.map (native)
    at resolveReflectiveProviders (D:\proj\node_modules\injection-js\injection.bundle.js:1307:31)
    at Function.ReflectiveInjector.resolve (D:\proj\node_modules\injection-js\injection.bundle.js:1504:16)
    at Function.ReflectiveInjector.resolveAndCreate (D:\proj\node_modules\injection-js\injection.bundle.js:1533:62)
    at Object.<anonymous> (D:\proj\src\index.ts:11:37)
    at Module._compile (module.js:571:32)
@mgechev
Copy link
Owner

mgechev commented Jul 28, 2017

You need the flag emitDecoratorMetadata.

@mgechev mgechev closed this as completed Jul 28, 2017
@galtalmor
Copy link
Contributor Author

Yep, now it works.
I think it's worth mentioning in the readme.
It would have saved me quite some time.
Thanks!

@mgechev
Copy link
Owner

mgechev commented Jul 28, 2017

@galtalmor would you open a PR?

@galtalmor
Copy link
Contributor Author

Sure. Will do.

@ningacoding
Copy link

Facing same problem with this tsconfig.json:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "jsx": "react-native",
    "lib": ["dom", "esnext"],
    "moduleResolution": "node",
    "noEmit": true,
    "skipLibCheck": true,
    "resolveJsonModule": true
  },
  "types": [
    "react",
    "react-native",
    "react-navigation",
    "jest"
  ]
}

Im ussing resolveAndCreate:

const injector = ReflectiveInjector.resolveAndCreate(lstServices);

HttpService:

@Injectable()
export class HttpService {

    constructor(private storageService: StorageService) {
    }

StorageService:


@Injectable()
export class StorageService {

Error: Cannot resolve all parameters for 'HttpService'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'HttpService' is decorated with Injectable.

@ningacoding
Copy link

Sorry, related to: #36

babel config.

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

3 participants