【问题标题】:How hide fields when using include filter使用包含过滤器时如何隐藏字段
【发布时间】:2021-03-19 19:14:36
【问题描述】:

每当我在过滤器中使用include 时,我都无法隐藏特定字段。例如下面的过滤器假设隐藏 listIdlist_id 字段,但它没有!

const filter = {
      where: {color: colorStr},
      include: [{relation: 'todos'}],
      fields: {"listId": false, "list_id": false}, //FIXME: Unable to hide these two fields
    }

输出是:

[
  {
    "id": "90caa22d-3d16-4d5f-9e15-13dd978a62a0",
    "title": "Home",
    "desc": "string",
    "color": 45,
    "status": 0,
    "todos": [
      {
        "id": "077b38f8-9bb6-41f1-aba5-fadae4ca98b3",
        "title": "Iron the t-shirt",
        "desc": null,
        "staus": 2,
        "listId": "90caa22d-3d16-4d5f-9e15-13dd978a62a0",
        "list_id": "90caa22d-3d16-4d5f-9e15-13dd978a62a0"
      },
      {
        "id": "26bb6473-fbd8-462d-92e6-efcb4a8de734",
        "title": "Clean the table",
        "desc": "string",
        "staus": 0,
        "listId": "90caa22d-3d16-4d5f-9e15-13dd978a62a0",
        "list_id": "90caa22d-3d16-4d5f-9e15-13dd978a62a0"
      },
      {
        "id": "9664d74f-f146-4b57-b34e-1bc668a063b6",
        "title": "Fıx the chaır",
        "desc": null,
        "staus": 2,
        "listId": "90caa22d-3d16-4d5f-9e15-13dd978a62a0",
        "list_id": "90caa22d-3d16-4d5f-9e15-13dd978a62a0"
      },
      {
        "id": "b8bde42b-182c-48aa-9597-a1da9961ea97",
        "title": "Fiixo",
        "desc": null,
        "staus": 4,
        "listId": "90caa22d-3d16-4d5f-9e15-13dd978a62a0",
        "list_id": "90caa22d-3d16-4d5f-9e15-13dd978a62a0"
      }
    ]
  }
]

【问题讨论】:

    标签: node.js typescript loopbackjs loopback4


    【解决方案1】:

    如果您要隐藏的字段与 todos 相邻,而不是待办事项数组中的子字段,我怀疑您的过滤器会起作用。

    您指定要包含的内容而不是要排除的内容的替代过滤器可能会起作用:

    const filter = {
      where: { color: colorStr },
      include: [
        {
          relation: 'todos',
          scope: {
            fields: ['id', 'title', 'desc', 'status'],
          },
        },
      ],
    };
    

    【讨论】:

    • 谢谢@Christian 我试过这个过滤器但我得到:error TS2345: Argument of type '{ where: { color: string; }; include: { relation: string; scope: { fields: string[]; }; }; }' is not assignable to parameter of type 'Filter<Lists>'. Types of property 'include' are incompatible. Type '{ relation: string; scope: { fields: string[]; }; }' is missing the following properties from type 'Inclusion[]': length, pop, push, concat, and 28 more.
    • 我的错,include 应该是一个数组。我更新了答案。
    • 不起作用;我不再能够得到todos 的输出[ { "id": "90caa22d-3d16-4d5f-9e15-13dd978a62a0", "title": "Home", "desc": "string", "color": 45, "status": 0 } ]
    【解决方案2】:

    scope 仅适用于过滤器对象的“根”。因此,我们需要使用点分隔语法来制作过滤器:

    // Due to a limitation with TypeScript types, we need to silence the compiler for this line.
    // @ts-ignore
    const filter: Filter<MyModel> = {
      where: {color: colorStr},
      include: [{relation: 'todos'}],
      fields: [
        'todos',
        'todos.listId',
        'todos.list_id',
      ],
    };
    

    参考文献

    【讨论】:

    • 我得到一个空的回复[ {}] 我错过了什么吗?我用我的真实模型Filter&lt;Lists&gt; 替换了Filter&lt;MyModel&gt; 我尝试运行Filter&lt;MyModel&gt;,但我得到了类似的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    相关资源
    最近更新 更多