Skip to content

Firestore onSnapshot with “where” and “orderBy” not matching any documents

I am struggling to figure out why the following code produces the “doc.empty” console log. The screenshot shows what I see in Cloud Firestore’s data console. My abbreviated code is below. In my example, I have the following variables:

dataset = '202203aam'
custnum = '19930'

enter image description here

firestoredb.collection('sold').doc(dataset).collection('sold').where('custnum', '==', parseInt(custnum)).orderBy('lot', 'asc').onSnapshot(function(doc){
    if (doc.empty){
    } else {
            //code here

Why would this not match my data?


The problem is here:

.where('custnum', '==', parseInt(custnum))

The screenshot shows that your custnum field has a string value, yet you are explicitly passing a numeric value in the condition. Strings and numeric values are never the same in the database, so the condition doesn’t match the document you how.

To make the query work, make sure you pass the value as the same type that you’ve stored in the database.