我正在使用Loopback4框架。我尝试将存储库注入某些模型类(将documentRepository注入userModelClass)。我不确定是否可以做这样的事情。我之所以想这样做,是因为在存储库中,我有一个方法可以返回分配给用户的所有文档。稍后会有更多的模型必须使用相同的方法。
这是我尝试注入存储库(构造函数用户模型)的方式:
constructor(
data?: Partial<User>,
@repository(DokumentRepository) public dokumentRepository?: DokumentRepository
)
启动应用程序时,我的控制台出现以下错误:
E:\workspace\compass\backend\node_modules\@loopback\repository\dist\decorators\repository.decorator.js:51
const stringOrModel = typeof modelOrRepo !== 'string' && !modelOrRepo.prototype.getId
^
TypeError: Cannot read property 'prototype' of undefined
at Object.repository (E:\workspace\compass\backend\node_modules\@loopback\repository\dist\decorators\repository.decorator.js:51:75)
at Object.<anonymous> (E:\workspace\compass\backend\dist\src\models\admin\user.model.js:266:29)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (E:\workspace\compass\backend\dist\src\repositories\admin\user.repository.js:19:22)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
Waiting for the debugger to disconnect...
Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)
也许你们中的某人有一个想法如何将存储库注入模型,或者可以告诉我为什么这不可能...
答案 0 :(得分:0)
我在使用完全相同的代码时遇到了一些麻烦,仅添加引号'DokumentRepository'
就解决了该问题。
@repository(DokumentRepository) public dokumentRepository?: DokumentRepository
经过Loopback 4 v 1.21.4
这是我的代码。
export class OwnerRepository extends DefaultCrudRepository<Owner, typeof Owner.prototype.ownerId, OwnerRelations> {
public readonly transactions: HasManyRepositoryFactory<Vehicle, typeof Vehicle.prototype.id>;
public readonly accounts: HasManyRepositoryFactory<Account, typeof Account.prototype.id>;
constructor(
@inject('datasources.mongo') dataSource: MongoDataSource,
@repository.getter('VehicleRepository') protected vehicleRepositoryGetter: Getter<VehicleRepository>,
@repository.getter('AccountRepository') protected accountsRepositoryGetter: Getter<AccountRepository>,
@repository('VehicleRepository') public vehicleRepository: VehicleRepository,
@repository('AccountRepository') public accountRepository: AccountRepository,
) {
super(Owner, dataSource);
this.vehicles = this.createHasManyRepositoryFactoryFor('vehicles', vehicleRepositoryGetter);
this.accounts = this.createHasManyRepositoryFactoryFor('accounts', accountsRepositoryGetter);
}