简体   繁体   中英

Firestore, adding value to an existing array field

I want the new ph value to be added in the arraylist but instead, it keep updating the first value of the arraylist

my firestore

var insBtn = document.getElementById("InsBtn");

 async function AddDocument_CustomID() {
    var ref = doc(db,"pHvalue", "pH");

     await setDoc(
        ref, {
            pH: [pH.value]
        });
}


insBtn.addEventListener('click', AddDocument_CustomID);

If you want to add a new unique value to the existing values in the pH array, you can use an array-union operation :

updateDoc(ref, {
    pH: arrayUnion(pH.value)
});

If you instead want to add a possibly non-unique value to the end of the pH array, you will instead have to:

  1. Load the document into your application and get the existing pH array from it.
  2. Add the new item to the end of the array.
  3. Write the entire array back to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM