How to query with distinct column?

Hi fellow Parse users and my apologies if there is already another topic for my question, i didn’t seem to find one.

I got a list of items and i simply want to get their distinct categories so i can have a filter by category on the frontend. So in the backend I did this:

const itemParse = Parse.Object.extend("Items");
const query = new Parse.Query(itemParse);
query.distinct('category');
query.find().then((itemQ: Parse.Object[]) => {
    const categories: Array<string> = [];
    itemQ.forEach((item: Parse.Object) => {
        categories.push(item.get('category'));
    })
    return { categories };
})

The issue is that i get an array of categories, but they are not distinct, their are lots of duplicates. I got something like: [“a”, “a”, “a”, “b”, “c”, “a”, “a”, “b”, “d”, “c”, “b”, “d”, …] but i would like to have [“a”, “b”, “c”, “d”, …].

In the docs, the example shows that you can use .then on the distinct function, but my typescript yells at me that i can’t use .then with distinct.

Am i doing something wrong?

You’d need to do just this to have all the distinct categories using plain javascript:

const itemParse = Parse.Object.extend("Items");
const query = new Parse.Query(itemParse);
const categories = await query.distinct('category');

Probably the typescript types are not updated for the distinct function and that’s why you are facing the problem. Do you mind to open an issue to the Parse JS SDK so we can figure it out? In the meantime, you can try something like this:

const categories = await (query as any).distinct('category');