简体   繁体   中英

How can i enter multiple data into documents in FIRESTORE

Currently when i enter a new value the old one gets overwritten ,i need to store all the entered values without overwriting. I am a beginner at this. thanks for the help.

const docRef = firestore.collection("samples").doc("itemData")
  const outputHeader = document.querySelector("#productList");
  const inputTextField = document.querySelector("#item");
  const saveButton = document.querySelector("#saveButton");

  saveButton.addEventListener("click",function() {
    const textToSave = inputTextField.value;
    docRef.set({
        product: textToSave
    }).then(function(){
        console.log("status saved!");
    }).catch(function(error){
        console.log("got an error",error);
    })

  })

Set your ref to the samples collection

const docRef = firestore.collection("samples");

Now in the button click code

docRef.add({
    product: textToSave,
    cost: costToSave
}).then(function(){
    console.log("status saved!");
}).catch(function(error){
    console.log("got an error",error);
})

this will create documents under samples collection with a unique key.

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