young-steveo / bottlejs

A powerful dependency injection micro container for JavaScript applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ServiceCopy is not a constructor

romanlex opened this issue · comments

Hello. I have this error with storejs library. How I can fix this?

const store = require('store');

export const STORAGE = 'storage';
export default store;
this._di.service('storage', Storage);

TypeError: ServiceCopy is not a constructor
GenericProvider.GenericFactory
webpack:///node_modules/bottlejs/dist/bottle.js:482
Object.getService
webpack:///node_modules/bottlejs/dist/bottle.js:368
getNested
webpack:///node_modules/bottlejs/dist/bottle.js:37
Array.reduce
(native)
Object.getNestedService
webpack:///node_modules/bottlejs/dist/bottle.js:61
Array.map
(native)
GenericProvider.GenericFactory
webpack:///node_modules/bottlejs/dist/bottle.js:478
Object.getService
webpack:///node_modules/bottlejs/dist/bottle.js:368
http://localhost:3000/js/main.js?3e3dddac462c00f3ed96:95365:29
Object.dispatch
webpack:///node_modules/redux-thunk/lib/index.js:11

It looks like require('store') does not return a constructor, but rather an already constructed instance, or an object literal. You can use a value or a factory for it instead of .service:

this._di.factory('storage', function() { return Storage; });
// or
this._di.value('storage', Storage);

bottle.service expects to be given an object that it can call new on, for example new Storage()