【问题标题】:Strapi v4 won't fetch image(or any media) columns from a related fieldStrapi v4 不会从相关字段中获取图像(或任何媒体)列
【发布时间】:2023-01-13 01:13:29
【问题描述】:

我正在尝试使用 slug 获取单个类别

http://localhost:1337/api/categories/{slug}

在我的控制器中:

 async findOne(ctx) {     
     const {id : slug} = ctx.params     
         const response = await strapi.db
           .query("api::category.category")
           .findOne({
             where: { slug: slug },
             populate: {
               blogs: {
                 select: ["id", "title"],
                 orderBy: ["id"],
               },
             },
           }); 

这很好用......但是当添加另一个字段时

select: ["id", "title", "image"], 

我得到错误

错误:选择不同的t1.blog_ordert0.idt0.idt0.titlet0.imaget1.category_id来自blogst0t0.id上以t1的身份加入categories_blogs_links = t1.blog_id 其中(t1.category_id 在 (2) 中)按 t0.id 排序 asc,t1.blog_orderasc - 没有这样的列:t0.image SqliteError: 选择不同的t1.blog_ordert0.idt0.idt0.titlet0.image, t1.category_id 来自blogs 作为t0 左加入 categories_blogs_links t1 t0.id = t1.blog_id 其中 (t1.category_id in (2)) 按t0.id asc, t1.blog_order排序 asc - 没有这样的列:t0.image

但是有一个字段叫做“图像”

【问题讨论】:

    标签: sqlite strapi


    【解决方案1】:

    populate 的用法真的很有趣,实际上没有想到您可以在 populate 中进行 orderBy 和 select 嗯。

    您遇到的问题可能是因为您还必须在内部明确填充关系。尝试这个:

    .findOne({
       where: { slug: slug },
       populate: {
         blogs: {
           populate: ["image"],
           select: ["id", "title", "image"],
           orderBy: ["id"],
         },
       },
    

    【讨论】:

      猜你喜欢
      • 2022-12-30
      • 2022-01-16
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 2023-01-15
      • 2022-07-10
      • 1970-01-01
      • 2022-08-03
      相关资源
      最近更新 更多