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