首页 > 解决方案 > Firestore 检查键是否存在,然后检查同一集合中的特定字段是否存在

问题描述

我的 Firebase firestore 是这样构建的:

duell -> documentID -> key:"l23dsk2ls",(player:uid), ...

Firestore 结构的屏幕截图

我检查密钥是否存在,如下所示:

   const db = firebase.firestore();
    var duettsRef = db.collection("duetts");
    export const keyExists = (setKey)=>{
    duettsRef
      .doc('key')
      .get().then(
        doc => {
          if (doc.exists) {
            this.db.collection('duett').doc('key').collection(setkey).get().
               then(sub => {
                  sub.docs.length > 0
          });
        }
      })
    }

如果存在,则密钥是唯一的。现在,而不是上面我只检查密钥是否存在的代码,我想知道密钥是否存在 -> 如果是 -> 如果player:"..."集合中有一个字段,我在其中找到了密钥。我怎样才能做到这一点?

标签: reactjsfirebasereact-nativegoogle-cloud-firestore

解决方案


要检查值是否存在,您可以使用 query,它看起来像这样:

duettsRef
  .where('player', '==', key)
  .get().then(
    snapshot => {
      if (!snapshot.empty) {

推荐阅读