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

232
Visualizações
Google Cloud Function delete Firestore document

I tested my javascript on local emulator. There are 2 actions, if Timestamp is over 7 days delete the document (this is working like a charm <3) but the else is he should check the "img" its a normal url if this return 404 then delete like on the first action, this is ONLY working on local emulator, when i deploy it to Cloud function its not working anymore and just says e.g "Function execution took 7957 ms, finished with status: 'ok'" BUT on local it works as expected.

exports.removeExpiredDocuments = functions.region('europe-west1').runWith({ memory: "256MB" }).pubsub.schedule("every 1 hours").onRun(async (context) => {
    const db = admin.firestore();
    const now = firestore.Timestamp.now();
    const ts = firestore.Timestamp.fromMillis(now.toMillis() - 604800000); // 168 hours in milliseconds = 604800000

    const snaps = await db.collection("products").get();
    let promises = [];
    snaps.forEach((snap) => {
        // functions.logger.info("forEachSnap");
        if (snap.data().created_time < ts) {
            promises.push(snap.ref.delete());
            functions.logger.info('[Time] older than 7 Days ' + snap.data().name, { structuredData: true });
        } else {
            requesth(snap.data().img, function (error, response) {
                functions.logger.info('[img] error: ' + error, { structuredData: true });
                if (response.statusCode == 404) {
                    promises.push(snap.ref.delete());
                    functions.logger.info('[img] not found ' + snap.data().name, { structuredData: true });
                }
            });
        }
    });

    return Promise.all(promises);
});

Working on local emulator (with same url as in firebase) and i expect that its working on cloud aswell

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

0

firestore.Timestamp.now() does not return a value that can be correctly compared to other Timestamps with < and > comparisons. That means this line of code doesn't do what you think:

if (snap.data().created_time < ts)

If you want to compare two Timestamp objects with each other, you must take another approach. One way is to convert them both to milliseconds with toMillis() and compare those integers with each other. Or convert them to Date and compare them with the appropriate object method.

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