Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Batch delete documents of a subcollection error: TypeError: n.indexOf is not a function

I am trying to batch delete a document of a subcollection.

There are times when I might need to delete the same product document ID and different history document ID or multiple product document IDs and different history document IDs.

I am passing the product document ID and history document ID, and I already see them in the console. However, I receive this error once I try to batch.delete() this:

This is what the docID and the historyId shows in the console:

enter image description here

import {
  doc,
  getDoc,
  updateDoc,
  deleteField,
  collection,
  getDocs,
  addDoc,
  increment,
} from "firebase/firestore";
import { db } from "../../Firebase/utilities";

async function deleteDocumentSubcollection(docID, historyId) {
try {
  const batch = writeBatch(db);

//I think this is where the error appears
  historyId.forEach((id) => {
    batch.delete(doc(db, "products", docID, "history", historyId));
  });

  await batch.commit();
  alert("deleted");
 

  console.log("deleted");
} catch (err) {
  console.log(err);
}
}

package.json

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As stated in the documentation, the docId and historyId should corresponds to the specific Ids of documents. You can try to delete specific documents using their corresponding IDs, and just a note, IDs should not be array:

historyId.forEach((id) => {
    batch.delete(doc(db, "products", "documentId", "history", id));
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!