2

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

この手順に従ってデータを取得しようとしましincludeたが、500 を取得しました。

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

私が欲しいのは、ゲームオブジェクトとそれに関連するランクを取得することです。

ランクモデル

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;

ゲームモデル

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;

ゲームコントローラ

// 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' }] });
  }
4

1 に答える 1