2

https://loopback.io/doc/en/lb4/HasMany-relation.html

I followed this steps and then tried to get data with include but I get 500.

500 Error: Invalid "filter.include" entries: {"relation":"ranks"}

What I want is to get games object with its related ranks.

Rank Model

import { Entity, model, property, belongsTo } from '@loopback/repository';
import { Game, GameWithRelations } from './game.model';

@model({ settings: { strict: 'filter' } })
export class Rank extends Entity {
  @property({
    type: 'string',
    id: true,
  })
  id?: string;

  @property({
    type: 'string',
  })
  name?: string;

  @property({
    type: 'string',
  })
  shortName?: string;

  @property({
    type: 'string',
  })
  avatar?: string;

  @belongsTo(() => Game)
  gameId: string;

  constructor(data?: Partial<Rank>) {
    super(data);
  }
}

export interface RankRelations {
  game?: GameWithRelations;
}

export type RankWithRelations = Rank & RankRelations;

Game Model

import { Entity, model, property, embedsMany, hasMany } from '@loopback/repository';
import { Rank, RankWithRelations } from './rank.model';
import { HasMany } from 'loopback-datasource-juggler';

@model({ settings: { strict: 'filter' } })
export class Game extends Entity {
  @property({
    type: 'string',
    id: true,
  })
  id?: string;

  @property({
    type: 'string',
    required: true,
  })
  name?: string;

  @property({
    type: 'string',
  })
  shortName?: string;

  @property({
    type: 'string',
  })
  avatar?: string;

  @hasMany<Rank>(() => Rank, { keyTo: 'gameId' })
  ranks?: Rank[];

  constructor(data?: Partial<Game>) {
    super(data);
  }
}

export interface GameRelations {
}

export type GameWithRelations = Game & GameRelations;

Game Controller

// in this method
// 500 Error: Invalid "filter.include" entries: {"relation":"ranks"}

 @get('/games/{id}')
  async findById(@param.path.string('id') id: string): Promise<Game> {
    return await this.gameRepository.findById(id, { include: [{ relation: 'ranks' }] });
  }
canmustu
  • 2,304
  • 4
  • 21
  • 34

1 Answers1

10

Please run your application with DEBUG=loopback:repository:relation-helpers, that way you will get a debug message explaining why filter.include entry was rejected.

You can find the code building the error message here:

https://github.com/strongloop/loopback-next/blob/97ba7893e253bfc2967ac08e408b211c9b9b7f40/packages/repository/src/relations/relation.helpers.ts#L96-L100

The most likely cause: your GameRepository does not have any InclusionResolver registered for ranks relation.

Please refer to our todo-list example to see how to register inclusion resolver. Cross-posting from https://github.com/strongloop/loopback-next/blob/97ba7893e253bfc2967ac08e408b211c9b9b7f40/examples/todo-list/src/repositories/todo-list.repository.ts#L41-L46:

this.todos = this.createHasManyRepositoryFactoryFor(
  'todos',
  todoRepositoryGetter,
);


this.registerInclusionResolver('todos', this.todos.inclusionResolver);
Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99
  • 1
    It is good to get answer from you. When I update loopback packages, `inclusionResolver` disappeared. Thanks. – canmustu Oct 05 '19 at 15:05