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

onDeactivation throws 'TypeError: Cannot convert undefined or null to object' #1462

Closed
saltylassi opened this issue May 25, 2022 · 3 comments

Comments

@saltylassi
Copy link

If container has both onActivation handler and onDeactivation, onDeactivation throws error.

Expected Behavior

working without error

Current Behavior

this error pops out

$ yarn build && node ./dist/main.js
$ tsc
blade instance Activation!
C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:453
        var constructor = Object.getPrototypeOf(instance).constructor;
                                 ^

TypeError: Cannot convert undefined or null to object
    at Function.getPrototypeOf (<anonymous>)
    at Container._deactivate (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:453:34)
    at Container._deactivateIfSingleton (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:634:21)
    at Container._deactivateSingletons (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:639:31)
    at Container.unbind (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:261:18)
    at Object.<anonymous> (C:\Users\user\Desktop\projects\inversify-test\dist\main.js:155:11)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)

Possible Solution

Steps to Reproduce (for bugs)

  1. follow these docs :

https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md#containeronactivationtserviceidentifier-interfacesserviceidentifiert-onactivation-interfacesbindingactivationt-void

https://github.com/inversify/InversifyJS/blob/master/wiki/container_api.md#ondeactivationtserviceidentifier-interfacesserviceidentifiert-ondeactivation-interfacesbindingdeactivationt-void

https://github.com/inversify/InversifyJS/blob/master/wiki/deactivation_handler.md#deactivation-handler

  1. run this sample :
@injectable()
export interface IBlade {
  sharpness: number;
}

export class Blade implements IBlade {
  constructor(@optional() public sharpness: number = 5) {}
}
const container = new Container();
container.bind<IBlade>(IDENTIFIERS.Blade).to(Blade).inSingletonScope();
container.onActivation(IDENTIFIERS.Blade, () => {
  console.log('blade instance Activation!');
});
container.onDeactivation(IDENTIFIERS.Blade, (blade: IBlade): void | Promise<void> => {
  console.log('blade instance deactivation!');
});

container.get<IBlade>(IDENTIFIERS.Blade);
container.unbind(IDENTIFIERS.Blade);
  1. error occurs
TypeError: Cannot convert undefined or null to object
    at Function.getPrototypeOf (<anonymous>)
    at Container._deactivate (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:453:34)
    at Container._deactivateIfSingleton (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:634:21)
    at Container._deactivateSingletons (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:639:31)
    at Container.unbind (C:\Users\user\Desktop\projects\inversify-test\node_modules\inversify\lib\container\container.js:261:18)
    at Object.<anonymous> (C:\Users\user\Desktop\projects\inversify-test\dist\main.js:155:11)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)

Context

Your Environment

  • Version used:
   "@types/node": "^17.0.35",
   "inversify": "^6.0.1",
   "reflect-metadata": "^0.1.13",
   "typescript": "^4.6.4"
  • Environment name and version (e.g. Chrome 39, node.js 5.4):
   node 14.17.0
  • Operating System and version (desktop or mobile):
   Windows 10
  • Link to your project:

Stack trace

@oscar60310
Copy link

I've got the same error message but with a different scenario. I accidentally bound "undefined" on the container:

import 'reflect-metadata';
import { Container } from 'inversify';
const container = new Container();
container.bind('Test').toConstantValue(undefined);
console.log(container.get('Test')); // undefined
container.unbindAll(); // Error: Cannot convert undefined or null to object

This issue can be resolved by removing improper binding.

@am0wa
Copy link

am0wa commented Nov 8, 2023

We're facing same error on container.unbindAll() call after inversify upgrade:

"inversify": "6.0.2",
"typescript": "4.9.5"

inversify6 0 2-error

@notaphplover
Copy link
Member

I would hazard to say this issue is already solved.

enum Identifier {
      Blade = 'Blade',
    }

    interface Blade {
      sharpness: number;
    }

    class BladeImpl implements Blade {
      constructor(@optional() @unmanaged() public sharpness: number = 5) {}
    }

    const container: Container = new Container();
    container.bind<Blade>(Identifier.Blade).to(BladeImpl).inSingletonScope();
    container.onActivation(Identifier.Blade, () => {
      console.log('blade instance Activation!');
    });
    container.onDeactivation(
      Identifier.Blade,
      (_blade: Blade): void | Promise<void> => {
        console.log('blade instance deactivation!');
      },
    );

    container.get<Blade>(Identifier.Blade);
    container.unbind(Identifier.Blade);

is currently working with the latest inversify version.

Regarding the runtime error, it should be solved since #1628.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

4 participants