首页 > 解决方案 > 我应该如何嵌入具有多对多关系的 2 个模型?

问题描述

我有两个具有多对多关系的模型:

所以每个博客都有一个tags属性,每个标签都有一个blogs属性。我还有两个 API 端点:/blogs 和 /tags

我的问题是,如果我在序列化程序中包含并嵌入模型,我会收到带有错误消息“超出最大调用堆栈大小”的循环引用

海市蜃楼 REPL

我想看到这样的东西:

GET /api/blogs
[
  {
    "id": "1",
    "title": "Some title",
    "author": "Author 0",
    "tags": [
      {
        "id": "1",
        "name": "Tag 0"
      },
      {
        "id": "2",
        "name": "Tag 1"
      }
    ]
  }
]

GET /api/tags
[
  {
    "id": "1",
    "name": "Tag 0",
    "blogs": [
      {
        "id": "1",
        "title": "Some title",
        "author": "Author 0"
      }
    ]
  },
  {
    "id": "2",
    "name": "Tag 1",
    "blogs": [
      {
        "id": "1",
        "title": "Some title",
        "author": "Author 0"
      }
    ]
  }
]

标签: javascriptmiragejs

解决方案


推荐阅读