简体   繁体   中英

how to delete all documents on firestore using javascript

i try this code for my project but on doc.reference.delete() is not working

 btnSubmit.addEventListener('click', e => {
    db.collection("ruangIGD")
    .get()
    .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // doc.data() is never undefined for query doc snapshots

            doc.reference.delete()
        });
    })
    .catch(function(error) {
        console.log("Error getting documents: ", error);
    });

how to solve this?

Try this, db have to be a const ! I think there is the problem

const db = new Firestore({
  projectId: "projectId",
  keyFilename: "./key.json"
 });
db.collection("collectionName")
  .get()
  .then(res => {
    res.forEach(element => {
      element.ref.delete();
    });
 });

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