2017-12-27 1169 views
0

有沒有一種方法來檢查是否存在一個子集合在firestore中爲nodejs?可以檢查一個集合或子集合是否存在?

目前我正在使用doc.exists作爲文檔,但我需要檢查子文檔是否存在於文檔中以編寫一些數據。

+0

我發現另一個答案,爲了這個偉大工程,是在這裏https://stackoverflow.com/questions/46880323/how-to-check-if-a-cloud-firestore-document-exists-when-using -realtime更新。這也適用於這裏描述的子集合https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/559 –

回答

3

是的,有。您可以使用docs.length來了解子集合是否存在。

我做了一個樣本來指導你,希望它有幫助。

this.db.collection('users').doc('uid') 
    .get().then(
    doc => { 
    if (doc.exists) { 
     this.db.collection('users').doc('uid').collection('friendsSubcollection').get(). 
     then(sub => { 
      if (sub.docs.length > 0) { 
      console.log('subcollection exists'); 
      } 
     }); 
    } 
    }); 
+0

這個答案真的幫我反正:) – Ahsath

+0

很好的答案!謝謝! – JeffMinsungKim

相關問題