反转:如何将模型 <Document> 绑定到容器对象
inversify: how to bind Model<Document> to container object
我正在开发一个应用程序,使用 typescript、mean stack、inversify-express-utils 和 inversifyjs 作为我的 IOC 容器。对于将模型添加到容器的条目,我在 inversify.config.ts 文件中收到一条错误消息。例如,
inversify.config.ts:
container.bind<Model<Document>>(TYPES.Document).to(User).whenTargetNamed("userSchema");
这似乎会导致以下错误:
"Argument of type Model is not assignable to parameter of type 'new (...args: any[]) => Model'. Type 'IUserModel' is not assignable type 'Model'. Property 'findByid' is missing in type 'IUserModel'."
igroupmodel.ts:
export interface IUserModel extends IUser, Document
types.ts:
Document: Symbol("Model")
group.js(最后一行):
export let User = mongoose.model<IUserModel>("User", UserSchema);
将猫鼬模型注入容器的正确方法是什么?我一直在寻找一个演示如何使用 inversifyjs 和 mongoose 的示例,但没有找到解决方案。
更新:
当我用 toConstantValue(User) 替换 to(User) 时,我不再收到错误。但是,我的日志文件似乎在我的日志文件中显示以下错误:
"Error: No matching bindings found for serviceIdentifier:
Model"
您可以找到 mongoose + inversify 的示例 here. The example uses the onion architecture,但如果您不喜欢它,您应该可以使用不同的架构...
我正在开发一个应用程序,使用 typescript、mean stack、inversify-express-utils 和 inversifyjs 作为我的 IOC 容器。对于将模型添加到容器的条目,我在 inversify.config.ts 文件中收到一条错误消息。例如,
inversify.config.ts:
container.bind<Model<Document>>(TYPES.Document).to(User).whenTargetNamed("userSchema");
这似乎会导致以下错误: "Argument of type Model is not assignable to parameter of type 'new (...args: any[]) => Model'. Type 'IUserModel' is not assignable type 'Model'. Property 'findByid' is missing in type 'IUserModel'."
igroupmodel.ts:
export interface IUserModel extends IUser, Document
types.ts:
Document: Symbol("Model")
group.js(最后一行):
export let User = mongoose.model<IUserModel>("User", UserSchema);
将猫鼬模型注入容器的正确方法是什么?我一直在寻找一个演示如何使用 inversifyjs 和 mongoose 的示例,但没有找到解决方案。
更新: 当我用 toConstantValue(User) 替换 to(User) 时,我不再收到错误。但是,我的日志文件似乎在我的日志文件中显示以下错误:
"Error: No matching bindings found for serviceIdentifier: Model"
您可以找到 mongoose + inversify 的示例 here. The example uses the onion architecture,但如果您不喜欢它,您应该可以使用不同的架构...