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

前端 未结 6 1644
野性不改
野性不改 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条回答
  •  我在风中等你
    2020-12-28 15:56

    Check this :)

      var doc = firestore.collection('some_collection').doc('some_doc');
      doc.get().then((docData) => {
        if (docData.exists) {
          // document exists (online/offline)
        } else {
          // document does not exist (only on online)
        }
      }).catch((fail) => {
        // Either
        // 1. failed to read due to some reason such as permission denied ( online )
        // 2. failed because document does not exists on local storage ( offline )
      });
    

提交回复
热议问题