0

I am learning dependecy injection using awilix. I tried the code below following a tutorial. I tried differently and each time I get the the kind of error below:

  //diSetup.js:13
  var config = _ref.config;
  TypeError: Cannot read property 'config' of undefined
  [Screenshot][1]

I tried the following:

const awilix = require("awilix");

const config = {
   server: "8.8.8.8",
};


class UserController {
   constructor({ config }) {
     this.config = config;
  }
}

const container = awilix.createContainer({
   injectionMode: awilix.InjectionMode.PROXY,
});


container.register({
  config: awilix.asValue(config),
  userController: awilix.asClass(UserController),
});

 function setup() {
    const user = new UserController();
    console.log(user.config);
 }

 module.exports = { 
    container, 
    setup,
 };
Ben Jonson
  • 565
  • 7
  • 22

1 Answers1

2

You are creating a UserController instance in your setup function.

You shound use awilix resolve function instead. Try this:

const user = container.resolve("userController");
Kai
  • 871
  • 2
  • 18
  • 39