zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
280 stars 34 forks source link

Unable to call injected object function in registered class object #152

Closed venkataramkishore closed 4 years ago

venkataramkishore commented 4 years ago

Tell us: What you are doing? Trying to execute document example.

Post a minimal code sample that reproduces the issue Attached source code

What do you expect to happen? Expected to call injected object method inside registered object.

What is actually happening? Unable to call injected object method "print"

"TypeError: this._animal.print is not a function"

Which NodeJS version you are using? PS D:\node_workspace\ditest> node -v v12.16.3 PS D:\node_workspace\ditest> npm -v 6.14.4

Which Ecma Script version you are using? Es6

Which Ecma Script presets you are using? i think default is support es6

Please help me with what i am doing wrong?

ditest.zip

venkataramkishore commented 4 years ago

I had to use new Reference('service.animal') in the index.js file. It worked ! But still not sure if that the correct?

Yml i have tried the yml file as well to understand the configuration. No luck

Error: Cannot find module 'src\Animal'

Error: Cannot find module 'src\Animal' Require stack: D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\JsFileLoader.js D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\index.js D:\node_workspace\ditest\index.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15) at Function.Module._load (internal/modules/cjs/loader.js:840:27) at Module.require (internal/modules/cjs/loader.js:1019:19) at require (internal/modules/cjs/helpers.js:77:18) at YamlFileLoader._requireClassNameFromPath (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:334:28) at YamlFileLoader._getDefinition (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:125:27) at YamlFileLoader._parseDefinition (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:87:47) at YamlFileLoader._parseDefinitions (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:68:14) at YamlFileLoader.load (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\YamlFileLoader.js:92:12) at Object. (D:\node_workspace\ditest\index.js:14:15) { code: 'MODULE_NOT_FOUND', requireStack: [ 'D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js', 'D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\JsFileLoader.js', 'D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\index.js', 'D:\node_workspace\ditest\index.js' ] }

zazoomauro commented 4 years ago

@venkataramkishore can you please add your code here with a simple example of what you are doing?

venkataramkishore commented 4 years ago

Hi @zazoomauro , I have attached my code as requested. Please do npm install first and npm start ditest.zip

Thank you for your help.

zazoomauro commented 4 years ago

@venkataramkishore Instead of uploading ditest.zip file I would like to see your code here like:

hello = 'bye bye'
function() {...}
venkataramkishore commented 4 years ago

Ok, The following is the code

# dependency-config.yml
services:
  service.animal:
    class: 'src/Animal'
  service.tiger:
    class: 'src/Tiger'
    arguments: ['@service.animal']
# index.js
const ContainerBuilder = require('node-dependency-injection').ContainerBuilder;
const YamlFileLoader = require('node-dependency-injection').YamlFileLoader;

let container = new ContainerBuilder()
let ymlFileLoader = new YamlFileLoader(container);

ymlFileLoader.load('./dependency-config.yml');
const mailer = container.get('service.tiger');
mailer.printMessage('Hello Node ..!');
# src/Animal.js
class Animal {
    constructor() {}

    print(msg) {
        console.log('Classone print message', msg);
    }
}
module.exports = Animal;
# src/Tiger.js
class Tiger {

    _animal;
    constructor(animal) {
        console.log('animal instance ', animal);
        this._animal = animal;
    }

    printMessage(message) {
        this._animal.print(message);
    }
}
module.exports = Tiger;

Finally with

# package.json
{
  "name": "ditest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-dependency-injection": "^2.6.8"
  }
}

After

npm install && npm start
Error: Cannot find module 'src\Animal'
Require stack:
- D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js
- D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\JsFileLoader.js
- D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\index.js
- D:\node_workspace\ditest\index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:957:15)
    at Function.Module._load (internal/modules/cjs/loader.js:840:27)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at YamlFileLoader._requireClassNameFromPath (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:334:28)
    at YamlFileLoader._getDefinition (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:125:27)
    at YamlFileLoader._parseDefinition (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:87:47)
    at YamlFileLoader._parseDefinitions (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\FileLoader.js:68:14)
    at YamlFileLoader.load (D:\node_workspace\ditest\node_modules\node-dependency-injection\dist\lib\Loader\YamlFileLoader.js:92:12)
    at Object.<anonymous> (D:\node_workspace\ditest\index.js:14:15) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'D:\\node_workspace\\ditest\\node_modules\\node-dependency-injection\\dist\\lib\\Loader\\FileLoader.js',
    'D:\\node_workspace\\ditest\\node_modules\\node-dependency-injection\\dist\\lib\\Loader\\JsFileLoader.js',
    'D:\\node_workspace\\ditest\\node_modules\\node-dependency-injection\\dist\\lib\\index.js',
    'D:\\node_workspace\\ditest\\index.js'
  ]
}
zazoomauro commented 4 years ago

@venkataramkishore I'll look in to it

venkataramkishore commented 4 years ago

Hi @zazoomauro , i tried the same it worked...! Thanks