代码之家  ›  专栏  ›  技术社区  ›  Paco Zevallos

添加/更新Firestore阵列

  •  0
  • Paco Zevallos  · 技术社区  · 5 年前

    我在Firestore工作。以下代码使用单个元素更新矩阵,即每次用户执行该函数时,所述字段都会更新:

    updateFavoritos(key) {
        const user = firebase.auth().currentUser;
        this.afs.doc('eventos/' + key).update({
          favoritos: [ user.uid ],
        });
      }
    

    这看起来像这样: enter image description here

    但是我怎样才能添加而不是更新矩阵呢,就是这样说:

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  0
  •   Marco    4 年前

    Firestore有处理数组的方法。链接到 docs

    var washingtonRef = db.collection("cities").doc("DC");
    
    // Atomically add a new region to the "regions" array field.
    washingtonRef.update({
        regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
    });
    
    // Atomically remove a region from the "regions" array field.
    washingtonRef.update({
        regions: firebase.firestore.FieldValue.arrayRemove("east_coast")
    });