question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'length' of undefined

See original GitHub issue

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.

 InternalDependencyContainer.prototype.resolve = function (token) { <--- token is undefined
        console.info("TOKEN", token)
        var registration = this.getRegistration(token);
        console.info("REGISTRATION", registration)
        if (!registration && isNormalToken(token)) {
            throw "Attempted to resolve unregistered dependency token: " + token.toString();
        }
        if (registration) {
            return this.resolveRegistration(registration);
        }
        return this.construct(token);
    };

To Reproduce

CLASS Causing the issue

@singleton()
export class EventService {
    private config: EventConfig
    private sns: SNS

    public constructor() {
        this.config = eventConfig
        this.sns = new SNS({
            endpoint: eventConfig.endpoint,
        })
    }

    public async publish(event: Event): Promise<void> {
        const opts: PublishInput = {
            Message: JSON.stringify(event),
            TopicArn: this.config.topicArn,
            Subject: event.type,
        }
        await this.sns.publish(opts).promise()
    }
}

ERROR:


TypeError: Cannot read property 'length' of undefined
    at InternalDependencyContainer.../../../node_modules/tsyringe/dist/esm5/dependency-container.js.InternalDependencyContainer.construct (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/tsyringe/dist/esm5/dependency-container.js:133:1)
    at InternalDependencyContainer.../../../node_modules/tsyringe/dist/esm5/dependency-container.js.InternalDependencyContainer.resolve (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/tsyringe/dist/esm5/dependency-container.js:67:1)
    at /Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/tsyringe/dist/esm5/dependency-container.js:147:1
    at Array.map (<anonymous>)
    at InternalDependencyContainer.../../../node_modules/tsyringe/dist/esm5/dependency-container.js.InternalDependencyContainer.construct (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/tsyringe/dist/esm5/dependency-container.js:140:1)
    at InternalDependencyContainer.../../../node_modules/tsyringe/dist/esm5/dependency-container.js.InternalDependencyContainer.resolve (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/tsyringe/dist/esm5/dependency-container.js:67:1)
    at Object../src/handlers/companies/listCompanyStages.ts (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/src/handlers/companies/listCompanyStages.ts:21:34)
    at __webpack_require__ (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/webpack/bootstrap:19:1)
    at /Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/webpack:/webpack/bootstrap:83:1
    at Object.<anonymous> (/Users/flowers_ben/Projects/ortus/ortus-monorepo/packages/api/client/.webpack/service/src/handlers/companies/listCompanyStages.js:87:10)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.createHandler (/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/serverless-offline/src/functionHelper.js:215:15)
    at handler (/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/serverless-offline/src/ApiGateway.js:485:40)
    at module.exports.internals.Manager.execute (/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/@hapi/hapi/lib/toolkit.js:41:33)
    at Object.internals.handler (/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/@hapi/hapi/lib/handler.js:46:48)
    at exports.execute (/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/@hapi/hapi/lib/handler.js:31:36)
    at Request._lifecycle (/Users/flowers_ben/Projects/ortus/ortus-monorepo/node_modules/@hapi/hapi/lib/request.js:312:68)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
benojcommented, Oct 9, 2019

ping

1reaction
skiptirengucommented, Oct 11, 2019

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);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read properties of undefined (reading 'length') - TrackJS
This message indicates that our code expects to have an object with a length property, but that object was not present. length is...
Read more >
TypeError: Cannot read property 'length' of Undefined in JS
The "Cannot read property 'length' of undefined" error occurs when accessing the length property on an undefined value. To solve the error, ...
Read more >
Typeerror: Cannot Read Property 'length' of Undefined
A typeerror length of undefined can indicate that the actual type of an operand is different from the expected type. This error can...
Read more >
[SOLVED] Cannot Read Property 'length' of Undefined in JS
4 Quick Fixes for the “Cannot Read Property 'length' of Undefined” Error in JavaScript · 1. Use an if Statement · 2. Use...
Read more >
Cannot read property length of undefined - Stack Overflow
The error means that what ever element is returned by getElementById('WallSearch') , it does not have a value property. Every form field has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found