Search code examples
javascriptphpdirectus

Directus API SDK: How to get items with M2M filter


I can't find (even in the docs) the best way to do an API call with M2M, from sdk-js. For example, if I have two collections "articles" and "categories" with an M2M relation. How do I get all the articles from one categorie_id ?

I've tried this :

directusClient.getItems(
  'articles',
   {
     filter: {
       categories: {
         id: req.params.id
       } 
     }
   }
)

But I get this error : Error: Unknown filter: id at new APIError.


Solution

  • I've found my error.. I just had to use filters instead of filter. Thus, I had to use the attribute categories_id instead of id.

    Final code :

    directusClient.getItems(
     'articles',
     {
       filters: {
         categories: {
           categories_id: req.params.id
         }
       }
     } 
    )