Korvent
Korvent

Reputation: 58

Loopback 4 include relational model in "parent" model

I'm actually building a REST API with Loopback and I would like to include the civility when I get a person (user). I created a hasOne relation but on the explorer, the GET /persons/{id}, I have the civilityId but not the civility itself..

Should I create it another way ?

Thanks,

Upvotes: 0

Views: 72

Answers (1)

Rifa Achrinza
Rifa Achrinza

Reputation: 1585

This can be caused by not registering the Relation Resolver or by disabling it.

To register the Relation Resolver, amend the Repository:

export class PersonRepository extends DefaultCrudRepository {
  account: HasOneRepositoryFactory<Account, typeof Person.prototype.id>;

  constructor(
    dataSource: juggler.DataSource,
    civilityRepositoryGetter: Getter<CivilityRepository>,
  ) {
    super(Person, dataSource);

    // we already have this line to create a HasOneRepository factory
    this.civility = this.createHasOneRepositoryFactoryFor(
      'account',
      civilityRepositoryGetter,
    );

    // add this line to register inclusion resolver
    this.registerInclusionResolver('civility', this.civility.inclusionResolver);
  }
}

Upvotes: 2

Related Questions