Search code examples
javascriptdirectusheadless-cms

How to do conditionally filtering with Directus Javascript SDK?


Problem

I am not able to change the default AND operator in the curl call like explained in the directus api docs:

https://docs.directus.io/api/reference.html#filtering

Question

How can I enable the OR operator in order to filter based on different criteria like the code below?

Code

return client
      .getItems('privileges', {
        fields: ['*', 'image.filename', 'image.data.full_url', 'attachment.*', 'partner.*.*', 'location.*', 'category.*.*', 'type.icon.data.full_url'],
        meta: ['total_count', 'result_count'],
        sort: 'created_on',

    filter: {
      'location.city': {
        'contains': lowerQ
      },
      // 'location.title': {
      //   'contains': lowerQ
      // },
      category: {
        'eq': category,
      },
      available_from: {
        'lte': 'now'
      },
      available_until: {
        'gte': 'now'
      },
    },
    limit,
    offset,
  }).then(response => {
    dispatch(getPrivilegesSuccess(key, response))
  }).catch(error => {
    console.log(error);
    dispatch(getPrivilegesFail(key, error))
  });

So, the comments out code is a different criteria that should be able to match on inserted text using the or operator. Instead it is using the AND operator in the curl output api call at the moment.

Would appreciate any type of help!


Solution

  • filter: {
      field_one: {
        'eq': 'hello'
      },
      field_two: {
        'logical': 'or',
        'eq': 'world'
      },
    },
    

    Just add 'logical': 'or' to the object you'd like to use as your 'OR'