Search code examples
node.jsstrapi

Strapi V4 filter where relation does not exist


I'm setting up a comments-like system where comments have a parent field which relations to another comment. I want to be able to query the top level comment by finding comments that have no parent set, but I cannot see in the documentation any way to do this.

I tried this, but it doesn't look like the $null filter works on joins.


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

And I get the error: Only $and, $or and $not can by used as root level operators. Found $null.

I can't find anything about this in the Strapi documentation but it seems like a pretty standard thing to do want to do.


Solution

  • So I found a solution to this using the Strapi query API rather than the entity service:

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