Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda