-
Notifications
You must be signed in to change notification settings - Fork 725
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
getAll - Error: No matching bindings found for serviceIdentifier #1469
Comments
Are there aany news on thi sissue? |
Old, but maybe this still helps you. A nasty fix is to monkey patch container.ts import { Container } from "inversify"
export const container = new Container()
// types taken from `inversify/lib/interfaces.d.ts`
export type Newable<T> = new (...args: any[]) => T;
export interface Abstract<T> {
prototype: T;
}
export type ServiceIdentifier<T = unknown> = (string | symbol | Newable<T> | Abstract<T>);
// monkey patching `container.getAll`
const getAll = container.getAll.bind(container)
container.getAll = <T>(serviceIdentifier: ServiceIdentifier<T>): T[] => {
try {
return getAll(serviceIdentifier)
}
catch (err) {
return []
}
} Now, this will no longer throw an error, but instead return an empty array: index.ts import { container } from "./container"
// class that is not bound.
class MyService {
}
async function main() {
let services = container.getAll(MyService)
console.log('🎉');
}
main() |
Sorry for the lack of response here, the project went through an unmaintained period but we have been publishing releases again recently ( -- Similar to #1359 I think this would be a great feature for the official library, imo we should aim to get this into an official release. cc @PodaruDragos |
Added to milestone |
This is not an issue. The current behavior is correct. Having 0 injected services in a multi injection is perfectly fine and might be even desired in some cases. |
@notaphplover OG post said that the current behaviour throws an error. |
Oh, sry, my bad then. Reopening the issue |
After having a look at the code, this is the expected behavior. |
Expected Behavior
If I have container that contains 0 bindings of class StartupCallback and I do
getAll
I get empty array.Current Behavior
If I have container that contains 0 bindings of class StartupCallback and I do
getAll
I get:Error: No matching bindings found for serviceIdentifier
Possible Solution
Get all should return empty array if 0 items are bound. Usually when resolving by all (array) it is for components like lifecycle callbacks and initializes which count may vary across environments. It should be possible to have between 0 and n of these instead of between 1 and n, as in environment without single callback we have to provide noop binding.
Your Environment
version: 6.0.1
The text was updated successfully, but these errors were encountered: