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

248
Visualizações
how can i delete multiple paths with firebase cloud functions

I want to delete data older than 15 days. Can I query and delete multiple directories at the same time?

The database structure is like this. There are multiple languages and types.

{
  "hr" : {
    "classic" : {
      "finish" : {
        "-MuJD4oAlQ42TkdfKW5M" : {
          "gid" : "LlAHMyj",
          "ts" : 6.652890289073215E8
        }
      }
    },
    "duel" : {
      "finish" : {
        "-MuJD4o9LONeayt4ACMC" : {
          "gid" : "rfePU2x",
          "ts" : 6.652890289073215E8
        }
      }
    },
    "target" : {
      "finish" : {
        "-MuJD4o9LONeayt4ACMD" : {
          "gid" : "FBz4H_4",
          "ts" : 6.652890289073215E8
        }
      }
    }
  },
  "nl" : {
    "target" : {
      "finish" : {
        "-MuJD4oAlQ42TkdfKW5L" : {
          "gid" : "sv3pRfC",
          "ts" : 6.652890289073215E8
        }
      }
    }
  }
}

I tried to do it with the code below but it always returns null.

exports.deleteOldData = function (snap, context) {
  const end = new Date(new Date().setDate(new Date().getDate() - 15));
  const endDate = toSwiftDate(end);

  database.ref("/matchTour/{language}/{type}/finish/").orderByChild("ts").endAt(endDate).transaction(function (info) {
    if (info === null) return info;
    console.log("matchTour 3", JSON.stringify(info.val()));
    return null;
  }).catch((error) => {
    console.log("error);
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can't run a transaction on a query. In fact, from looking at the reference documentation for Query this shouldn't even compile.

This should be closer:

exports.deleteOldData = function (snap, context) {
  const end = new Date(new Date().setDate(new Date().getDate() - 15));
  const endDate = toSwiftDate(end);

  const ref = database.ref("/matchTour/{language}/{type}/finish/");
  return ref
    .orderByChild("ts").endAt(endDate)
    .once("value")
    .then((results) => {
    let updates = {};
    results.forEach((snapshot) => {
      updates[snapshot.key] = null;
    });
    return ref.update(updates);
  }).catch((error) => {
    console.error(error);
  });
};

The above reads the results for the query from the data, performs a single multi-path update to delete all of them, and ensures the the promises returned by the asynchronous read and write bubble up, so that your Functions container doesn't get terminated prematurely.


That leaves the question of what language and type are in database.ref("/matchTour/{language}/{type}/finish/"). If there are meant to be variables that come from the context, you'll want to use backticks for this string:

database.ref(`/matchTour/{language}/{type}/finish/`)

All in all there are quite a few mistakes in this code that make me think you may not yet be too comfortable with writing server-side JavaScript. If that is the case, writing Cloud Functions is not the easiest way to get started, and I recommend first writing the same code in a client-side JavaScript environment, like a local node.js script, or even a web page.

Reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer are great starting points.

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