Reputation: 1
i am using the Loopback4 framework. I try to inject a repository into some model class (documentRepository into userModelClass). I am not sure if it is possible to do something like that. The reason why i want to do this is because in the repository i have a method which returns all documents that are assigned to an user. Later there will be more models which have to use the same method.
This is the way i tried to inject my repository (constructor user model):
constructor(
data?: Partial<User>,
@repository(DokumentRepository) public dokumentRepository?: DokumentRepository
)
When i am starting the application i am getting the following error in my console:
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)
Maybe someone of you have an idea how to inject a repository to a model or can tell my why that is not possible...
Upvotes: 0
Views: 745
Reputation: 1147
I had some troubles with exact same code and just adding quotations 'DokumentRepository'
fixed the issue.
@repository(DokumentRepository) public dokumentRepository?: DokumentRepository
Tested on Loopback 4 v 1.21.4
Here's my code.
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);
}
Upvotes: 2