#How can I properly utilize filters in directus sdk

14 messages · Page 1 of 1 (latest)

drifting sky
#

Hey all,

I'm currently facing difficulties with implementing a filter in Directus and I'm unable to achieve the desired result.

Here's the code I'm using:
const { data: investments } = await useAsyncData('investments', () => { return $directus .items('investments') .readOne(1, { fields: [ 'title', 'subtitle', 'investments.sort', 'investments.item.header_img', 'investments.item.title', 'investments.item.subtitle', 'investments.item.type', 'investments.item.square_footage_max', 'investments.item.square_footage_min', ], }); });

The code returns:
{ "title": "Wynajem", "subtitle": "Otwórz swój lokal w najbardziej prestiżowych lokalizacjach Gdyni.", "investments": [ { "sort": 1, "item": { "header_img": "68b4de94-0219-4425-9fd9-96e63334f385", "title": "Plac Kaszubski", "subtitle": "W samym sercu miasta", "type": [ "usługowy", "gastronomiczny" ], "square_footage_max": "250", "square_footage_min": "250" } }, // ... } ] }

The investment field is M2A. I'm specifically trying to filter by the square_footage_min and square_footage_max variables, but I'm encountering issues accessing these values. I've attempted to use filter and deep methods. While I managed to apply filtering successfully using deep for testing purposes with the sort option, I'm struggling to achieve the desired effect when applying the filter to the fields in item. In some cases, I get a null response, while in others, all items are returned.

deep: { investments: { _filter: { sort: { _eq: 1 } } }, }

How to get to for example square_footage_max from deep or any other value under item?

thorn steepleBOT
#

Thanks for posting! This is a community powered server, so you may or may not get an answer based on available help and expertise. To increase your chances of somebody being able to help you, please help us help you making sure you:

  • Adding an explanation of exactly what you're trying to achieve.
  • Adding any and all related code or previous attempts.
  • Describing the exact issue or error you are facing.
  • Posting any screenshots if applicable.
  • Reading through https://stackoverflow.com/help/how-to-ask.

When you're done with this thread, please close it. Thanks! ✨

(If you have a support agreement and need help, please contact the core team via email.)

formal lark
#

i think you'll be wanting to use readByQuery, not readOne

drifting sky
#

Thank you @formal lark for your reply. I also attempted to use the readByQuery , but I couldn't observe any noticeable difference in the results between using filter and deep options. Unfortunately, I'm still facing the issue where almost all items are consistently returned, regardless of the filter applied.

dapper silo
#

it's a recursive m2a?

#

can you build the query in the Directus app

#

try this with readByQuery:

readByQuery({
    filter:{
    investments:{
        "item:investments":{
            square_footage_min: {
                _eq:250
            },
            square_footage_max: {
                _eq:250
            }
        }
    }
}
})
drifting sky
#

Thanks @dapper silo, with your query I'm getting an empty object

  "data": {}
}```

Code:
```  const { data: investments } = await useAsyncData('investments', () => {
    return $directus
      .items('investments')
      .readByQuery({
        fields: [
          'title',
          'subtitle',
          'investments.sort',
          'investments.item.header_img',
          'investments.item.title',
          'investments.item.subtitle',
          'investments.item.type',
          'investments.item.square_footage_max',
          'investments.item.square_footage_min',
        ],
        filter:{
          investments:{
              "item:investments":{
                  square_footage_min: {
                      _eq:250
                  },
                  square_footage_max: {
                      _eq:250
                  }
              }
          }
        }
      });
  });```

I'm fairly new to Directus, so I'm not sure how I can build the query in the Directus app?

The `investments` field is related to two collection `multiple_premises` and `single_premises`
#

here's the output of the query using *.*.* instead of selecting specific fields

dapper silo
#

You can build the query here

drifting sky
#

I can apply filters directly within the single_premises or multiple_premises collections. However, the investments collection is treated as a single object so the query builder cannot be used. Ideally, I would prefer to hide the single_premisses and multiple_premisses collections from the user and manage them through the investments collection, which includes the M2A investments field.

Same with the SDK, instead of fetching single_premisses and multiple_premisses separately, I would like to fetch only the investments collection, which already includes both of these collections. But I don't yet know how to do this filtering correctly.

dapper silo
#

this is how the filter works:
{{server}}/items/pages?filter[blocks][item:block_gallery][title][_contains]=a

where:
blocks = your M2A field name
item:block_gallery = the target collection type
title= the field inside the target collection

#

you may run into some issues when adding multiple filters, because it is essentially a one to many

dapper silo
#

Did this help?