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

145
Visualizações
Delete a batch of docs using admin SDK and callable cloud function in most similar way to client SDK?

I have an array of docs ids that I want to delete in using a cloud function, my code looks like the following :

//If the user decieds on deleting his account forever we need to make sure we wont have any thing left inside of db after this !!

// incoming payload array of 3 docs
data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']}

export const deleteClients = functions.https.onCall(async (data, context) => {
  try {
    // declare batch
    const batch = db.batch();

    // set
    data.arr.forEach((doc: string) => {
      batch.delete(db.collection('Data'), doc);
    });

    // commit 
    await batch.commit();
    
  } catch (e) {
    console.log(e);
  }
  return null;
});
I am getting a syntax error on batch.delete how to pass the right arguments in to the batch delete to reference that doc I want to submit for deletion before commit ?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Delete takes a single param, the doc ref of the doc to be deleted.

data.arr.forEach((docId: string) => {
  batch.delete(doc(db, "Data", docId));
});
about 4 years ago · Juan Pablo Isaza Relatório

0

There are several errors in your code:

  • data.arr.forEach() cannot work wince your data object contains one element with the key array and not the key arr.
  • You are mixing up the syntax of the JS SDK v9 and the Admin SDK. See the write batch Admin SDK syntax here.
  • You need to send back some data to the client when all the asynchronous work is complete, to correctly terminate your CF.
  • You do return null; AFTER the try/catch block: this means that, in most of the cases, your Cloud Function will be terminated before asynchronous work is complete (see the link above)

So the following should do the trick (untested):

const db = admin.firestore();

const data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']};

export const deleteClients = functions.https.onCall(async (data, context) => {
  try {
    
    const batch = db.batch();
    const parentCollection = db.collection('Data')
   
    data.array.forEach((docId) => {   
      batch.delete(parentCollection.doc(docId));
    });

    // commit 
    await batch.commit();
    
    return {result: 'success'} // IMPORTANT, see explanations above
    
  } catch (e) {
    console.log(e);
    // IMPORTANT See https://firebase.google.com/docs/functions/callable#handle_errors
  }
  
});
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