0

我的项目中,有两个实体;ProfileProject

实体Project具有字段“collaborators”,它是Profile. ->代码

EntityProfile有两个@Expose字段,“昵称”和“id”,当我调用 GET Profile API 时,这两个字段显示得很好。->代码 -> 查看dev.colact.io/api/profiles/8的结果。

{
    "createdDt": "2021-10-05T14:51:29.312Z",
    "updatedDt": "2021-10-05T14:51:29.312Z",
    "deletedDt": null,
    "photo": "https://collact-dev.s3.ap-northeast-2.amazonaws.com/profiles/oj2UBVFXCTnZ9z9mmy8UtU-59FD9F11-1812-4758-85B7-4347A4CE0FEF.png",
    "contact": null,
    "intro": null,
    "detail": null,
    "email": null,
    "phone": null,
    "projects": [],
    "nickname": "Tester",
    "id": 8
}

但是,当我调用 GET Project API 时,暴露的字段“昵称”和“id”不会出现在“合作者”中。-> 查看dev.colact.io/api/projects/72的结果。

{
    "createdDt": "2021-12-20T18:14:22.277Z",
    "updatedDt": "2021-12-20T18:14:22.277Z",
    "deletedDt": null,
    "id": 72,
    "photos": [
        {
            "createdDt": "2021-12-20T18:14:22.321Z",
            "updatedDt": "2021-12-20T18:14:22.321Z",
            "deletedDt": null,
            "id": 26,
            "photo": "https://collact-dev.s3.ap-northeast-2.amazonaws.com/projects/cWk3fQEoperPxAS6T1QV5K.png"
        }
    ],
    "collaborators": [
        {
            "createdDt": "2021-10-03T14:53:20.165Z",
            "updatedDt": "2021-12-09T13:26:11.870Z",
            "deletedDt": null,
            "photo": "https://collact-dev.s3.ap-northeast-2.amazonaws.com/profiles/oj2UBVFXCTnZ9z9mmy8UtU-59FD9F11-1812-4758-85B7-4347A4CE0FEF.png",
            "contact": "ㅆZz",
            "intro": "Zz",
            "detail": "Asdfasdf",
            "email": null,
            "phone": null
        }
    ],
    "areas": [
        {
            "createdDt": "2021-07-12T13:28:12.063Z",
            "updatedDt": "2021-07-12T13:28:12.063Z",
            "deletedDt": null,
            "id": 1,
            "area": "drawing"
        },
        {
            "createdDt": "2021-07-12T13:28:12.063Z",
            "updatedDt": "2021-07-12T13:28:12.063Z",
            "deletedDt": null,
            "id": 2,
            "area": "design"
        }
    ],
    "title": "title",
    "detail": ""
}

我已经尝试了 2 种解决方案,但没有按我的意愿工作。

首先,我尝试将 @Type(() => Profile) 添加到Project实体中的“协作者”,但没有区别。

其次,我认为 join 只能工作到collaborators,所以需要collaborators.user像下面这样加入。

  findOne(id: string): Promise<Project> {
    return this.projectsRepository.findOne(id, {
      relations: ['collaborators', 'collaborators.user', 'areas', 'photos'],
    });
  }

然而,这一次,“昵称”和“id”并没有出现在合作者身上。相反,嵌套的用户对象作为一个整体出现了。

{
    "createdDt": "2021-10-03T11:49:51.052Z",
    "updatedDt": "2021-10-03T11:49:51.052Z",
    "deletedDt": null,
    "id": 4,
    "title": "title2",
    "detail": "",
    "collaborators": [
        {
            "createdDt": "2021-10-03T11:46:20.347Z",
            "updatedDt": "2021-10-03T11:46:20.347Z",
            "deletedDt": null,
            "photo": null,
            "contact": null,
            "intro": null,
            "detail": null,
            "email": null,
            "phone": null,
            "user": {
                "createdDt": "2021-10-03T11:46:20.347Z",
                "updatedDt": "2021-10-03T11:46:20.347Z",
                "deletedDt": null,
                "id": 1,
                "nickname": "string1",
                "isSuperuser": false,
                "isStaff": false,
                "isActive": true,
                "loginPlatform": "manual",
                "uid": "string",
                "token": "string",
                "lastLoginDt": "2021-10-03T11:46:20.347Z"
            }
        }
    ],
    "areas": [
        {
            "createdDt": "2021-10-03T11:44:05.962Z",
            "updatedDt": "2021-10-03T11:44:05.962Z",
            "deletedDt": null,
            "id": 1,
            "area": "drawing"
        },
        {
            "createdDt": "2021-10-03T11:44:05.962Z",
            "updatedDt": "2021-10-03T11:44:05.962Z",
            "deletedDt": null,
            "id": 2,
            "area": "design"
        }
    ],
    "photos": []
}

有人可以帮忙吗?

4

1 回答 1

0

添加@UseInterceptors(ClassSerializerInterceptor)到控制器是关键。

另外,我上面提到的第二种解决方案(加入)应该在前面。

于 2022-01-03T15:24:18.843 回答