What's the best way to check if a Firestore record exists if its path is known?

前端 未结 6 1645
野性不改
野性不改 2020-12-28 15:15

Given a given Firestore path what\'s the easiest and most elegant way to check if that record exists or not short of creating a document observable and subscribing to it?

6条回答
  •  猫巷女王i
    2020-12-28 16:06

    Depending on which library you are using, it may be an observable instead of a promise. Only a promise will have the 'then' statement. You can use the 'doc' method instead of the collection.doc method, or toPromise() etc. Here is an example with the doc method:

    let userRef = this.afs.firestore.doc(`users/${uid}`)
    .get()
    .then((doc) => {
      if (!doc.exists) {
    
      } else {
    
      }
    });
    
    })
    

    Hope this helps...

提交回复
热议问题