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

250
Visualizações
node.js / firebase functions) I'm having a hard time comparing the two times

I used 'firebase functions' to create a schedule function that periodically calls and deletes files in storage when they are old. I want to compare the file's 'timeCreated' with the current time and make it delete after 10 minutes more since the file was created. The following is part of my code.

functions/index.js

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const firebase = admin.initializeApp();

exports.scheduledFunction = functions
  .region("asia-northeast3")
  .pubsub.schedule("every 1 minutes")
  .onRun(async (context) => {
    try {
      const bucket = firebase.storage().bucket();

      // get files
      const [filesArray] = await bucket.getFiles({
        prefix: "chat/files", // storage file path
      });

      const TIMESTAMP_AGO = Date.now() - 600000; // before 10min 
      const DELETE_OPTIONS = { ignoreNotFound: true }; // ??
.
.
      filesArray.map(async (file) => {
        let metadata;

        try {
          // metadata
          [metadata] = await file.getMetadata();
          const { temporaryHold, eventBasedHold, timeCreated } = metadata;

          const dispose = timeCreated < TIMESTAMP_AGO; // is file older check

           if (dispose) {
              await file.delete(DELETE_OPTIONS); 
              console.log("delete file");
            }
        }
  }
}

I would like to check if the creation time is more than 10 minutes by comparing the time below. However, 'dispose' is always returned as false.

const TIMESTAMP_AGO = Date.now() - 600000;
const dispose = timeCreated < TIMESTAMP_AGO; // always false

The following is the log output from the firebase functions console.

timeCreated: 2022-05-10T14:46:08.014Z, TIMESTAMP_AGO: 1652196423265 

What should I do to compare the time correctly?

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

0

The problem is that you are trying to compare datetime with timestamp.
A solution would be to turn the TIMESTAMP_AGO TO dataTime:

const TIMESTAMP_AGO = Date.now() - 600000;
const time_10mins_ago = new Date(TIMESTAMP_AGO);
const dispose = timeCreated < time_10mins_ago; 
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