【问题标题】:Strapi V4 filter where relation does not exist不存在关系的 Strapi V4 过滤器
【发布时间】:2022-11-11 18:49:14
【问题描述】:

我正在建立一个类似 cmets 的系统,其中 cmets 有一个与另一条评论相关的 parent 字段。我希望能够通过查找没有设置 parent 的 cmets 来查询顶级评论,但我在文档中看不到任何方法来做到这一点。

我试过这个,但看起来$null 过滤器不适用于连接。


            const posts = await strapi.entityService.findMany('api::post.post', {
                filters: {
                    thread: {
                        id: {
                            $eq: entity[0].id,
                        }
                    },
                    parent: {
                        $null: true,
                    }
                }
            });

我得到了错误:只有 $and、$or 和 $not 可以用作根级运算符。发现 $null。

我在 Strapi 文档中找不到任何关于此的内容,但这似乎是一件非常标准的事情。

【问题讨论】:

    标签: node.js strapi


    【解决方案1】:

    所以我找到了一个使用 Strapi 查询 API 而不是实体服务的解决方案:

                const posts = await strapi.db.query('api::post.post').findMany({
                    where: {
                        thread: entity[0].id,
                        parent: null
                    }
                });
    

    【讨论】:

    • Strapi v3有什么办法吗?
    【解决方案2】:
    filters[relation_name][id][$null]=true
    or
    filters:{
      relation_name:{
        id:{
          $null:true
        }
      }
    }
    

    这有助于我过滤掉关系为空/不存在的记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2022-12-15
      • 2020-07-18
      相关资源
      最近更新 更多