David Seek
David Seek

Reputation: 17132

How to form Firestore query matching with undefined elements

I'm trying to perform the following query with Firebase's Firestore:

async function getTicketNumbers(companyID) {

    var ticketNumbers = []

    const docs = await store
        .collection('ABC')
        .doc(companyID)
        .collection('DEF')
        .where('ticketStatus', 'in', ['scheduled', undefined])
        .get()

    if (docs) {
        docs.forEach(doc => {
            ticketNumbers.push(doc.id)
        })
    }

    return ticketNumbers
}

The problem in my code is the undefined keyword. But how can I perform a query where TicketStatus is either scheduled or undefined?

I also tried null and empty string '', what does not result in an error, but does not return additional documents. Any help is highly appreciated.

Upvotes: 6

Views: 6282

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317750

There is no "undefined" value for Cloud Firestore fields. The valid types can be seen in the documentation. You can see that null is a valid field value, but not undefined.

If you're trying to query for fields that don't exist at all, that is not possible. Firestore can only search for fields participating in an index, and a lack of a field means a lack of indexing. You will have to arrange for every document to have a field with some value if you want it to be queryable.

Upvotes: 13

Related Questions