How to check if current user has filled out a document?

Viewed 43

I have two collections, one is users and the other is invoice details. On user login, I need to check that the current user has filled out all fields in the invoice details collection. So need to show an alert as soon as user logs in to say to fill out details.

Both collections have the same UID's

checkJob = () => {

  firebase.firestore().collection('invoice_details').where("accountname", "==", "").get().then((resultSnapShot) => {


          if (resultSnapShot.size == 0) {

              Alert.alert("Details need to be updated")

          } else {

              Alert.alert("Details ok")
          }

      })

  }
1 Answers

If your invoice document generate after user submit some data then you can check if document exists of that user.

var docRef = db.collection("invoice_details").doc(userId);

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});
Related