【问题标题】:How to Make Relation in LoopBack v4 With Include Key如何使用包含键在 LoopBack v4 中建立关系
【发布时间】:2020-01-30 05:42:26
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js mongodb loopbackjs loopback4


    【解决方案1】:

    请使用DEBUG=loopback:repository:relation-helpers 运行您的应用程序,这样您将收到一条调试消息,解释为什么filter.include 条目被拒绝。

    您可以在此处找到构建错误消息的代码:

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

    最可能的原因:您的GameRepository 没有为ranks 关系注册任何InclusionResolver。

    请参阅我们的todo-list 示例,了解如何注册包含解析器。来自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);
    

    【讨论】:

    • 很高兴得到您的答复。当我更新环回包时,inclusionResolver 消失了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2018-07-01
    • 1970-01-01
    • 2019-03-26
    • 2023-03-16
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多