dynexx
dynexx

Reputation: 119

firebase does not catch errors

I am trying to get the error to work but for some reason, it doesn't say so. I did when the user put the phone number it will find it, and if he finds it he will return the doc data and the id, And it is working. although the err does not work.

const onSubmit = (data) => {
    const phoneExists = docRef.collection('Guests').where("guestPhone", '==', data.phonenumber).get();
    phoneExists.then(function(querySnapshot) {
        querySnapshot.forEach(doc => {
            console.log(doc.id, "=>", doc.data());
            // docRef.collection('Guests').doc(doc.id).update({
            //     numOfInvites: data.guestsnumber,
            //     confirmed: true
            // })
        })
    }).catch(err => {
        console.log('Error getting documents', err);
        console.error('Error getting documents', err);
        
    })
}

it's not return anything while the phone number does not exist in the document.

Upvotes: 0

Views: 202

Answers (1)

Yash Desai
Yash Desai

Reputation: 60

Firebase only returns an error when there was a problem in getting the request like the internet was down or the user didn't have enough permission to get the document. If you want to check if no document exists, you can use querySnapshot.empty which returns a boolean

const empty = querySnapshot.empty

You can also use the size operator to check the number of documents returned

const numberOfDocuments = querySnapshot.size

You can Learn more about all properties available here https://firebase.google.com/docs/reference/js/firebase.firestore.QuerySnapshot#properties_1

Upvotes: 1

Related Questions