0

我正在使用 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)

也许你们中的某个人知道如何将存储库注入模型,或者可以告诉我为什么这是不可能的......

4

1 回答 1

2

我在使用完全相同的代码时遇到了一些麻烦,只需添加引号即可'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);
  }
于 2019-09-02T09:23:46.140 回答