首页 > 解决方案 > Strapi 关系不返回正确的层次结构

问题描述

我正在尝试创建以下关系模型:

Section has many categories
Categories has many sub-categories

所以基本上我们在这里有三个集合:

  1. 部分
  2. 类别
  3. 子类别

首先,我定义了Section和之间的关系Category

在此处输入图像描述

然后,我创建了Categoryand之间的关系SubCategory

在此处输入图像描述

我也填充了这些集合,当我打电话时,http://localhost:3002/sections我得到:

[
    {
        "id": 1,
        "name": "ambition",
        "published_at": "2020-11-20T10:21:08.000Z",
        "created_at": "2020-11-20T11:21:06.000Z",
        "updated_at": "2020-12-01T10:26:44.000Z",
        "description": null,
        "featured_image": null,
        "categories": [],
        "subcategories": []
    },
    {
        "id": 2,
        "name": "the_tanning_of_the_future",
        "published_at": "2020-11-20T10:21:19.000Z",
        "created_at": "2020-11-20T11:21:18.000Z",
        "updated_at": "2020-12-01T10:27:15.000Z",
        "description": null,
        "featured_image": null,
        "categories": [],
        "subcategories": []
    },
    {
        "id": 3,
        "name": "living_sustainable",
        "published_at": "2020-11-20T10:21:28.000Z",
        "created_at": "2020-11-20T11:21:26.000Z",
        "updated_at": "2020-12-01T10:27:31.000Z",
        "description": null,
        "featured_image": null,
        "categories": [],
        "subcategories": []
    },
    {
        "id": 4,
        "name": "creativity",
        "published_at": "2020-11-20T10:21:36.000Z",
        "created_at": "2020-11-20T11:21:34.000Z",
        "updated_at": "2020-12-04T10:53:32.000Z",
        "description": null,
        "featured_image": null,
        "categories": [
            {
                "id": 1,
                "name": "Archivio",
                "published_at": "2020-11-20T10:57:34.000Z",
                "created_at": "2020-11-20T11:22:48.000Z",
                "updated_at": "2020-12-04T10:50:50.000Z",
                "subcategory": null,
                "feature_image": null
            },
            {
                "id": 2,
                "name": "Colori",
                "published_at": "2020-11-20T10:57:37.000Z",
                "created_at": "2020-11-20T11:22:55.000Z",
                "updated_at": "2020-12-03T11:55:11.000Z",
                "subcategory": null,
                "feature_image": null
            },
            {
                "id": 3,
                "name": "Lifestyle",
                "published_at": "2020-11-20T10:57:39.000Z",
                "created_at": "2020-11-20T11:23:02.000Z",
                "updated_at": "2020-12-03T11:55:20.000Z",
                "subcategory": null,
                "feature_image": null
            }
        ],
        "subcategories": [
            {
                "id": 1,
                "name": "Inverno 21",
                "published_at": "2020-12-03T15:16:57.000Z",
                "created_at": "2020-12-03T11:52:13.000Z",
                "updated_at": "2020-12-03T15:16:57.000Z",
                "category": 1,
                "description": null
            },
            {
                "id": 2,
                "name": "Estate 21",
                "published_at": "2020-12-03T11:54:42.000Z",
                "created_at": "2020-12-03T11:54:36.000Z",
                "updated_at": "2020-12-03T15:14:04.000Z",
                "category": 1,
                "description": null
            }
        ]
    }
]

从响应中可以看出,Section和之间的关系工作正常,但和Category之间的关系不正常。事实上,我已经定义了这些模型之间的 1:n 关系,但我得到:CategorySubCategory

{
    "id": 4,
    "name": "creativity",
    "published_at": "2020-11-20T10:21:36.000Z",
    "created_at": "2020-11-20T11:21:34.000Z",
    "updated_at": "2020-12-04T10:53:32.000Z",
    "description": null,
    "featured_image": null,
    "categories": [
        {
            "id": 1,
            "name": "Archivio",
            "published_at": "2020-11-20T10:57:34.000Z",
            "created_at": "2020-11-20T11:22:48.000Z",
            "updated_at": "2020-12-04T10:50:50.000Z",
            "subcategory": null,
            "feature_image": null
        },

另一个奇怪的事情是,在仪表板中,我实际上可以将子类别与 selected 相关联Category,请参阅:

在此处输入图像描述

所以我应该有回应:

{
    "id": 4,
    "name": "creativity",
    "published_at": "2020-11-20T10:21:36.000Z",
    "created_at": "2020-11-20T11:21:34.000Z",
    "updated_at": "2020-12-04T10:53:32.000Z",
    "description": null,
    "featured_image": null,
    "categories": [
        {
            "id": 1,
            "name": "Archivio",
            "published_at": "2020-11-20T10:57:34.000Z",
            "created_at": "2020-11-20T11:22:48.000Z",
            "updated_at": "2020-12-04T10:50:50.000Z",
            "subcategories": [
                ...
            ],
            "feature_image": null
        },

我做错了什么还是 Strapi 的关系引擎有问题?

标签: node.jsstrapi

解决方案


推荐阅读