sqlite Strapiv4不会从相关字段中提取图像(或任何媒体)列

wvmv3b1j  于 2023-01-13  发布在  SQLite
关注(0)|答案(2)|浏览(114)

我正在尝试使用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"],

我得到了错误
错误:选择不同的t1blog_ordert0idt0idt0titlet0imaget1category_idblogs作为t0向左加入categories_blogs_links作为t0上的t1id = t1blog_id其中((2)中的t1. category_id)按t0. id升序、t1. blog_order升序排序-无此列:t0.图像方型错误:选择不同的t1blog_ordert0idt0idt0titlet0imaget1category_idblogs作为t0向左加入categories_blogs_links作为t1t0id = t1blog_id其中((2)中的t1. category_id)按t0. id升序、t1. blog_order升序排序-无此类列:t0.image
但是有一个字段叫做"图像"

bweufnob

bweufnob1#

很有趣的填充用法,其实没想到你可以在填充里面排序和选择。
你遇到的问题很可能是因为你必须显式地填充内部的关系。

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

egdjgwm82#

我得到这个错误的原因是因为媒体字段没有存储在数据库中,所以sqlite正在寻找一个不存在的列。所以我们可能必须单独填充它。

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

相关问题