In strapi v3 I’ve used this:
const {
convertRestQueryParams,
buildQuery,
} = require('strapi-utils');
const exportQuery = buildQuery({
model: strapi.query('products').model,
filters: convertRestQueryParams({
_where: {},
_limit: -1,
}),
rest: undefined,
})
const response = await strapi
.query('products')
.model
.query((qb) => {
exportQuery(qb);
qb.select('category')
.distinct()
.catch((error) => {
strapi.log.error(error);
return null;
});
})
.fetchAll({ withRelated: ['category'] });
const categories = response.toJSON().map(result => result.category);