【问题标题】:Strapi: How to include/populate relations with fetch call?Strapi:如何包含/填充与 fetch 调用的关系?
【发布时间】:2022-01-12 08:09:58
【问题描述】:

我正在尝试根据关系“博客类别”为“博客文章”单曲制作“相关文章”,但我似乎无法让这些关系出现在 api 调用中。 获取与 Strapi 关系的正确参数的 API url 是什么?

要使用图像按日期降序填充所有文章,我正在这样做:

const res = 
   await fetch (
       https://localhost:3000/api/blog-articles?populate=*&sort[0]=publishDate%3Adesc
   )

Collection's Content Fields

编辑:Fetch result 编辑 2:Schema

【问题讨论】:

  • 抱歉,问题有点不清楚。所以你有 2 个集合,即 blog-articlesblog-categoriesmany-to-many 关系相关。您对 fetch API 的响应需要什么? “我正在尝试根据“博客类别”关系为“博客文章”单曲制作“相关文章”是什么意思?
  • @Salvino 抱歉,问题不清楚。我的问题是:如何通过 api fetch 调用获取 blog-articles 的相关 blog-categories 字段。

标签: fetch content-management-system strapi


【解决方案1】:

我认为您可以通过在populate http 查询字符串中传递相关字段名称来实现此目的。

HTTP 请求网址
GET http://localhost:3000/api/blog-articles?populate=blog_categories&sort[0]=publishedAt%3Adesc
JS 实现
const resp = await fetch('http://localhost:3000/api/blog-articles?populate=blog_categories&sort[0]=publishedAt%3Adesc');
const data = await resp.json();
console.log(data);

【讨论】:

  • 不幸的是不起作用。显示与之前相同的信息,但这次没有缩略图信息。
  • 您能在您的问题中发布您的博客文章模型设置 json 文件吗?
  • 将 schema.json 文件添加到原始帖子中。这是具体字段:“attributes”:{“blog_categories”:{“type”:“relation”,“relation”:“manyToMany”,“target”:“api::blog-category.blog-category”,“inversedBy ": "blog_articles" },
  • 是的,我的查询似乎是正确的。你能检查类别中的数据吗?检查是否有发布记录..
猜你喜欢
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2015-05-27
  • 2022-07-08
  • 2021-05-08
  • 2022-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多