gpt4 book ai didi

node.js - InversifyJS : Error when retrieving object from container

转载 作者:太空宇宙 更新时间:2023-11-04 00:28:09 26 4
gpt4 key购买 nike

我正在使用 InversifyJS 设置某种动态绑定(bind)并在 NodeJS 上运行它。我遇到的问题是我收到错误,但没有消息,只有堆栈跟踪。

Main

export class Main {
public static getCalculator(config: string): Calculator {
try {
var container: Container = new KeyFeatureContainer(config).getContainer();
debugger;
// error here !
return container.get<Calculator>(TYPES.Calculator);
} catch (error) {
debugger;
return null;
}
}
}

此类调用 KeyFeatureContainerjson字符串,用于配置所有绑定(bind)(以某种动态方式),然后检索容器。

KeyFeatureContainer

export class KeyFeatureContainer {
private _container: Container;

public constructor(config: string) {
var jsonConfig: any[] = JSON.parse(config);

this._container = new Container();
this._container.bind<Calculator>(TYPES.Calculator).to(KeyFeatureCalculator);

for (var i = 0; i < jsonConfig.length; i++) {
if (jsonConfig[i].active) {
this.parseConfigKeyFeatures(jsonConfig[i].id);
this.parseConfigParams(jsonConfig[i].params);
}
}
}

public getContainer(): Container {
debugger;
return this._container;
}

private parseConfigKeyFeatures(id: string): void {
var keyFeatureContainerModule: ContainerModule = rootContainer.get<KeyFeatureContainerModule>(id).getContainerModule();

if (keyFeatureContainerModule != null)
this._container.load(keyFeatureContainerModule);
}

private parseConfigParams(params: Array<{ name: string, value: any }>): void {
for (var param of params)
this._container.bind(param.name).to(param.value);
}
}

该类收到 json配置和加载,遵循 ID,ContainerModule其中包含接口(interface)和具体实现之间的绑定(bind)。 rootContainer指定ContainerModule检索以下 ID。

rootContainer

var rootContainer: Container = new Container();

rootContainer.bind<KeyFeatureContainerModule>(KEYFEATURES.DrugHoliday).to(DrugHolidayContainerModule);
rootContainer.bind<KeyFeatureContainerModule>(KEYFEATURES.MissingDay).to(MissingDayContainerModule);

export default rootContainer;

以及相关的ContainerModule s(第二个是相同的,只是每个参数不同)

@injectable()
export class MissingDayKeyFeature implements KeyFeature {
@inject(PARAMS.MissingDayParams.NbIntakesLimit)
private _nbIntakesLimit: number;

@inject(PARAMS.MissingDayParams.ExtraParamA)
private _extraParamA: any;

@inject(PARAMS.MissingDayParams.ExtraParamB)
private _extraParamB: any;

public init(): void {
console.log("init() at MissingDay");

console.log("nbIntakesLimit = " + this._nbIntakesLimit);
console.log("extraParamA = " + this._extraParamA);
console.log("extraParamB = " + this._extraParamB);
}

public calculate(): void {
console.log("calculate() at MissingDay");
}

public finish(): void {
console.log("finish() at MissingDay");
}
}

@injectable()
export class MissingDayContainerModule implements KeyFeatureContainerModule {
public getContainerModule(): ContainerModule {
return new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind) => {
bind<KeyFeature>(TYPES.KeyFeature).to(MissingDayKeyFeature);
});
}
}

最后,ServiceIdentifiers用于设置整个(单独的文件)

let TYPES = {
KeyFeature: "KeyFeature",
Calculator: "Calculator"
}

export default TYPES;

let PARAMS = {
DrugHolidayParams: {
NbDaysLimit: "nbDaysLimit",
ExtraParamA: "extraParamDHA",
ExtraParamB: "extraParamDHB"
},
MissingDayParams: {
NbIntakesLimit: "nbIntakesLimit",
ExtraParamA: "extraParamMDA",
ExtraParamB: "extraParamMDB"
}
}

export default PARAMS;

let KEYFEATURES = {
MissingDay: "MissingDayKeyFeature",
DrugHoliday: "DrugHolidayKeyFeature"
}

export default KEYFEATURES;

最后是JSON输入(为了方便而格式化)

[{
"id": "DrugHolidayKeyFeature",
"active": true,
"params": [{
"name": "nbDaysLimit",
"value": 3
}, {
"name": "extraParamDHA",
"value": "DHA"
}, {
"name": "extraParamDHB",
"value": "DHB"
}
]
}, {
"id": "MissingDayKeyFeature",
"active": false,
"params": [{
"name": "nbIntakesLimit",
"value": 0
}, {
"name": "extraParamMDA",
"value": "MDA"
}, {
"name": "extraParamMDB",
"value": "MDB"
}
]
}
]

我使用一个简单的Test脚本只是为了输出值并查看所有绑定(bind)是否正确完成

import { Main } from "./Main";
var json: string = *json above*;
Main.getCalculator(json).calculate();

但我最终遇到了这个错误

Error
< at _createSubRequests (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\planning\planner.js:106:19)
< at Object.plan (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\planning\planner.js:125:5)
< at D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\container\container.js:205:37
< at Container._get (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\container\container.js:198:44)
< at Container.get (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\node_modules\inversify\lib\container\container.js:163:21)
< at Function.getCalculator (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\transpiled\KFCalc\Main.js:9:30)
< at Object.<anonymous> (D:\Projects\226RD\nodejs\phantomjs_2016_by_DM\final\transpiled\KFCalc\Test.js:4:13)
< at Module._compile (module.js:573:32)
< at Object.Module._extensions..js (module.js:582:10)
< at Module.load (module.js:490:32)

我不知道发生了什么,也不知道我做错了什么。我不能get<Calculator>也不get<KeyFeature> ,但如果我这样做:

container.isBound(TYPES.Calculator) // returns true !!!

这同样适用于所有绑定(bind)项目。

感谢您的指点,我没有主意。

+-- @types/node@7.0.2
+-- inversify@3.0.0
+-- reflect-metadata@0.1.9
+-- typescript@2.0.10

编辑:忘记了KeyFeatureCalculator

@injectable()
export class KeyFeatureCalculator implements Calculator {
// multi-injection of bound key features
private _keyFeatureCalculators: KeyFeature[] = [];

public constructor(@multiInject(TYPES.KeyFeature) keyFeatureCalculators: KeyFeature[]) {
this._keyFeatureCalculators = keyFeatureCalculators;
}

public calculate(): void {
console.log("calculate() at KeyFeatureCalculator");

for (var calculator of this._keyFeatureCalculators) {
calculator.init();
calculator.calculate();
calculator.finish();
}
}
}

此外,我的所有文件(或几乎)都导入 inject, injectable来自inversify并且还导入reflect-metadata

最佳答案

错误已解决:对于那些没有注意的人来说,每个KeyFeature对象都注入(inject)了其属性,并且它们的类型是numberany。由于这些值是从我的 JSON 中检索的,因此它们的类型为 any,因此编译器不会警告我绑定(bind) bind(xxx).to( yyy) 需要是一个 newable 元素!

因此,在我的示例中,yyy 类似于 3,它是一个数字,因此不受限制。我更改为 bind(xxx).toConstantValue(yyy) 并且一切正常!

关于node.js - InversifyJS : Error when retrieving object from container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com