Closed benoj closed 4 years ago
For now I can get around it by labelling the class with @inject("eventService")
and initialising in the top level - but this isn't ideal
ping
Can you provide a valid test case to reproduce this issue? Tried these two and the container seems to be able to resolve the dependencies just fine.
test("test empty constructor 1", () => {
@singleton()
class EventService {
private config: object;
private sns: object;
public constructor() {
this.config = {};
this.sns = {};
}
public async publish(): Promise<void> {
console.log(this.config);
console.log(this.sns);
}
}
globalContainer.resolve(EventService);
});
and
test("test empty constructor 2", () => {
@singleton()
class EventService {
private config: object;
private sns: object;
public constructor() {
this.config = {};
this.sns = {};
}
public async publish(): Promise<void> {
console.log(this.config);
console.log(this.sns);
}
}
@singleton()
class MyClass {
constructor(@inject(EventService) service: EventService) {
console.log(service);
}
}
globalContainer.resolve(MyClass);
});
I just encountered a similar Cannot read property 'length'
error. I've narrowed it to usage of an index.ts
file, but it's not clear to me why that's causing issues.
src/
services/
ServiceA.ts
ServiceB.ts
ServiceC.ts
index.ts
main.ts
services/index.ts
export * from "./ServiceA"
export * from "./ServiceB"
export * from "./ServiceC"
ServiceA
and ServiceB
aren't dependent on any other services.
ServiceC.ts
//...
import { ServiceA, ServiceB } from "./"; // ⛔ Doesn't work.
//import { ServiceA } from "./ServiceA"; // ✅
//import { ServiceB } from "./ServiceB"; // ✅ Everything works fine.
@singleton()
export class ServiceC {
constructor(private a : ServiceA, private b : ServiceB) { }
//...
}
main.ts
// How they're imported here doesn't seem to make a difference
import { ServiceA, ServiceB, ServiceC } from "./services";
container.resolve(ServiceA); // ✅
container.resolve(ServiceC); // ⛔ Cannot read property 'length' of undefined
Logging the container in main.ts
does show that ServiceC
is in the registry.
Uncaught TypeError: Cannot read property 'length' of undefined
at InternalDependencyContainer.construct (dependency-container.js:129)
at InternalDependencyContainer.resolve (dependency-container.js:72)
EDIT: Well, I just created a reduced test case with StackBlitz, and I'm not seeing any problems with it. My project is a Chrome extension content script so that might be causing an issue or it could be webpack.
I've created a webpack + typescript project to try and reproduce this bug but was unable to narrow down the issue.
If I have to guess this is probably caused by some shady webpack/babel/etc plugin.
Closing due to lack of activity
This is still an issue. Confirmed today - replace indirect imports to direct imports and that fixed our weird bugs.
This is still an issue. Confirmed today - replace indirect imports to direct imports and that fixed our weird bugs.
Can you shed more light on this? I currently have this issue trying to deploy a solidity smart contract. I traced the error back to a folder in my node_module\hardhat-deploy\src\helpers.ts but still can't get rid of the error. If you could just explain what you meant by replacing indirect imports with direct one's
Describe the bug When I have a class with an empty constructor container blows up. I have a class which requires the CLASS (below).
This causes the whole container to blow up with an undefined.length issue.
To Reproduce
CLASS Causing the issue
ERROR: