user11810458
user11810458

Reputation: 33

node.js firestore delete with a where clause

I've tried to figure this out but don't know enough about node.js or firestore to make it happen. I want to delete a bunch of documents based on a where clause. I have tried a bunch of iterations of this but none of them delete, and using the Google provided code returns an async error:

db.collection("testcollection")
.where("office", "==", 12345).limit(3).get().then((snapshot)=> {

 snapshot.docs.forEach(doc => {
  //const res = await db.collection('testcollection').doc(id).delete();
    const data = doc.data();
        console.log(doc.id);
        delete(doc.id);
       
  })
});```

Upvotes: 0

Views: 324

Answers (1)

user11810458
user11810458

Reputation: 33

Well shoot. Right after posting this I figured it out by using an answer from here Cloud Firestore delete function and putting doc.id into a variable:

snapshot.docs.forEach(doc => {
 const tempid = doc.id;
    db.collection("testcollection").doc(tempid).delete().then(function() {
      console.log("Document successfully deleted!");
    }).catch(function(error) {
      console.error("Error removing document: ", error);
    });
       
  })
});

Upvotes: 1

Related Questions