You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Strapi 4.1.8中afterCreate hook调用REST API时无法获取关联数据

Strapi 4.1.8 REST API创建数据后afterCreate Hook无法获取关联数据问题

数据表结构

拥有一个包含1个关联关系的数据表(关联Country表)

问题描述

通过REST API新增数据时,afterCreate hook的返回结果中无法看到关联的Country数据;但通过Strapi管理后台新增数据时,能正常获取该关联数据。

实际情况

管理后台新增场景

  • beforeCreate hook接收的参数:
{
  "first_name": "John",
  "last_name": "from strapi local",
  "phone_number": "123456",
  "email": "member@email.test",
  "number_of_property_managed": "3",
  "country": 1,
  "createdBy": 1,
  "updatedBy": 1,
  "publishedAt": null,
  "createdAt": "2022-08-26T08:57:11.542Z",
  "updatedAt": "2022-08-26T08:57:11.542Z"
}
  • afterCreate hook的返回结果:
{
  "id": 34,
  "first_name": "John",
  "last_name": "from strapi local",
  "email": "member@email.test",
  "phone_number": "123456",
  "number_of_property_managed": "3",
  "createdAt": "2022-08-26T08:57:11.542Z",
  "updatedAt": "2022-08-26T08:57:11.542Z",
  "publishedAt": null,
  "country": {
    "id": 1,
    "name": "Malaysia",
    "createdAt": "2022-05-11T19:23:28.047Z",
    "updatedAt": "2022-05-11T19:23:28.835Z",
    "publishedAt": "2022-05-11T19:23:28.815Z"
  },
  "createdBy": {
    "id": 1,
    ...
  },
  "updatedBy": {
    "id": 1,
    ...
  }
}

此时Country关联数据正常显示。

REST API新增场景

  • beforeCreate hook接收的参数:
{
  "first_name": "John",
  "last_name": "from FE local",
  "email": "member@email.test",
  "phone_number": "018888888888",
  "country": 1,
  "number_of_property_managed": "10",
  "publishedAt": "2022-08-26T08:55:02.879Z",
  "createdAt": "2022-08-26T08:55:02.884Z",
  "updatedAt": "2022-08-26T08:55:02.884Z"
}
  • afterCreate hook的返回结果:
{
  "id": 33,
  "first_name": "John",
  "last_name": "from FE local",
  "email": "member@email.test",
  "phone_number": "018888888888",
  "number_of_property_managed": "10",
  "createdAt": "2022-08-26T08:55:02.884Z",
  "updatedAt": "2022-08-26T08:55:02.884Z",
  "publishedAt": "2022-08-26T08:55:02.879Z"
}

此时Country关联数据未加载。

需求

希望通过REST API创建数据后,能在afterCreate hook中访问关联的Country数据。

说明

当前使用Strapi版本为4.1.8,已搜索相关问题,但大多未区分操作场景,旧版本管理后台创建时的关联数据问题据称在3.0.0版本已修复,但当前问题出现在REST API创建场景。

内容的提问来源于stack exchange,提问作者Dimas Natanael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 03:03:36