简体   繁体   English

使用实时更新时如何检查云 Firestore 文档是否存在

[英]How to check if a cloud firestore document exists when using realtime updates

This works:这有效:

db.collection('users').doc('id').get()
  .then((docSnapshot) => {
    if (docSnapshot.exists) {
      db.collection('users').doc('id')
        .onSnapshot((doc) => {
          // do stuff with the data
        });
    }
  });

... but it seems verbose. ......但似乎很冗长。 I tried doc.exists , but that didn't work.我试过doc.exists ,但这没有用。 I just want to check if the document exists, before subscribing to realtime updates on it.我只想在订阅文档的实时更新之前检查文档是否存在。 That initial get seems like a wasted call to the db.最初的 get 似乎是对数据库的浪费调用。

Is there a better way?有没有更好的办法?

Your initial approach is right, but it may be less verbose to assign the document reference to a variable like so:您最初的方法是正确的,但将文档引用分配给像这样的变量可能不那么冗长:

const usersRef = db.collection('users').doc('id')

usersRef.get()
  .then((docSnapshot) => {
    if (docSnapshot.exists) {
      usersRef.onSnapshot((doc) => {
        // do stuff with the data
      });
    } else {
      usersRef.set({...}) // create the document
    }
});

Reference: Get a document参考: 获取文档

In case you are using PHP-Firestore integration, like me, write something like this:如果您像我一样使用 PHP-Firestore 集成,请编写如下内容:

$firestore = new FirestoreClient();
$document = $firestore->document('users/john');
$snapshot = $document->snapshot();

if ($snapshot->exists()) {
   echo "John is there!";
}

Enjoy!享受! o/ o/

Please check following code.请检查以下代码。 It may help you.它可能会帮助你。

 const userDocRef = await firestore().collection('collection_name').doc('doc_id');
   const doc = await userDocRef.get();
   if (!doc.exists) {
     console.log('No such document exista!');
   } else {
     console.log('Document data:', doc.data());
   }

I know its late, but it's actually snapshot.empty not snapshot.exists .我知道它迟到了,但它实际上是 snapshot.empty 而不是 snapshot.exists 。 snapshot.empty checks if a doc exists and returns accordingly. snapshot.empty 检查文档是否存在并相应返回。 happy coding 😊快乐编码😊

请注意,在火力地堡V9(模块化SDK) exists是一种方法,而不是一个属性(它会给你falsy结果所有的时间),按照该文档

If you are using Angular you can use the AngularFire package to check it with an observable. 如果您使用的是Angular,则可以使用AngularFire包以可观察的方式对其进行检查。

import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';

constructor(private db: AngularFirestore) {
  const ref: AngularFirestoreDocument<any> = db.doc(`users/id`);
  ref.get().subscribe(snap => {
    if (snap.exists) {
      // ...
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 React 中的实时更新从 Firestore 文档参考中获取数据 - How to get data from Firestore Document Reference with realtime updates in React 使用Cloud Firestore获取整个馆藏的实时更新 - Get realtime updates with Cloud Firestore on entire collection 使用 Cloud Firestore 获取有关实时更新的先前数据 - Get previous data on realtime updates with Cloud Firestore 对单个 Firestore 文档进行实时更新 - Have realtime updates for a single Firestore document 如何检查firestore文档中是否存在字段? - How to check whether field exists or not in the firestore document? 无法检查云 Firestore 数据库中是否存在文档以及 reactjs - Can't check if document exists in cloud firestore database and with reactjs Firestore 如何使用 javascript 检查云 Firestore 中的数组中是否存在元素 - Firestore how do I check if an element exists in an array in cloud firestore using javascript 如何从Firebase Firestore获取带有change.type ==“ added&#39;的实时文档更新 - How do I get Realtime document updates from Firebase Firestore with change.type =="added' 如何使用云功能删除 Firestore 中的文档 - How to delete a document in Firestore using cloud functions 如何获取firestore中子集合的实时更新? - How to get realtime updates for a subcollection in firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM