how to update date variable every midnight in node js express server to fetch from new collection `data-${date}`

huangapple go评论63阅读模式
英文:

how to update date variable every midnight in node js express server to fetch from new collection `data-${date}`

问题

我有一个Express服务器,我需要从Firebase获取一个新的集合,其格式为data-${date}。我如何使用Node Schedule(或任何其他方法)来完成这个任务。

代码主要格式如下:

let date = new Date();

app.get("/fetch", (req, res) => {
    // 从Firestore数据库中获取`data-${date}`的数据
}

简而言之:需要每天午夜更新date变量。

英文:

I have an express server server where I need to fetch new collection from firebase that is of formate data-${date} . How can I use node schedule (or anyother method) to accomplish this task .

Code is mainly of format


let date=new Date();

app.get("/fetch",(req,res) => {
        // fetch data from firestore db `data-${date}`
}

tldr: date variable needs to be updated every midnight

答案1

得分: 4

以下是翻译好的部分:

你可以在Node.js Express服务器中每天午夜更新日期以从新的集合中获取数据

const express = require("express");
const admin = require("firebase-admin");
const moment = require("moment");

const app = express();

admin.initializeApp({
  credential: admin.credential.cert("path/to/serviceAccountKey.json"), // 替换为您的服务帐户密钥文件的路径
});

const db = admin.firestore();

const baseCollectionName = "data";

let currentDate = moment().format("YYYY-MM-DD");

const fetchData = () => {
 const collectionName = `${baseCollectionName}-${currentDate}`;

 const collectionRef = db.collection(collectionName);

 // 在此处执行您想要的集合操作(例如,获取数据)
 // ...
};

// 函数用于在午夜时更新currentDate变量
const updateDateAtMidnight = () => {
 const nextMidnight = moment().endOf("day");
 const duration = moment.duration(nextMidnight.diff(moment()));

 const millisecondsUntilMidnight = duration.asMilliseconds();

 setTimeout(() => {
    currentDate = moment().format("YYYY-MM-DD");
    updateDateAtMidnight();
 }, millisecondsUntilMidnight);
};

updateDateAtMidnight();

app.get("/fetch", (req, res) => {
  fetchData();
});

app.listen(3000, () => {
 console.log("服务器已启动,端口号为3000");
});
英文:

You can update date every midnight in node js express server to fetch from new collection

const express = require("express");
const admin = require("firebase-admin");
const moment = require("moment");

const app = express();

admin.initializeApp({
  credential: admin.credential.cert("path/to/serviceAccountKey.json"), // 
  Replace with the path to your service account key file
});

const db = admin.firestore();

const baseCollectionName = "data";

let currentDate = moment().format("YYYY-MM-DD");

const fetchData = () => {
 const collectionName = `${baseCollectionName}-${currentDate}`;

 const collectionRef = db.collection(collectionName);

// Perform your desired operations on the collection here (e.g., fetching 
data)
 // ...
};

// Function to update the currentDate variable at midnight
const updateDateAtMidnight = () => {
 const nextMidnight = moment().endOf("day");
 const duration = moment.duration(nextMidnight.diff(moment()));

 const millisecondsUntilMidnight = duration.asMilliseconds();

 setTimeout(() => {
    currentDate = moment().format("YYYY-MM-DD");
    updateDateAtMidnight();
 }, millisecondsUntilMidnight);
};

updateDateAtMidnight();

app.get("/fetch", (req, res) => {
  fetchData();
});

app.listen(3000, () => {
 console.log("Server started on port 3000");
});

答案2

得分: 1

节点调度器(Node scheduler)像许多其他调度器一样基于 cronJobs。

因此,您需要定义 cronSettings:

const cronSettings = '0 0 * * *';
// 将您的函数附加到调度器

const scheduledJob = schedule.scheduleJob(cronSettings, functionToUpdate)

您似乎使用了一个 Express 端点,这很好,如果您也想手动触发的话。

const functionToUpdate = (req, res){
   // 做一些事情
}

app.get("/fetch", functionToUpdate)

希望对您有所帮助。

Balint

英文:

node scheduler like many other schedulers are based on cronJobs.

So you need to define a cronSettings

const cronSettings = '0 0 * * *'
// attach your function to the scheduler

const scheduledJob = schedule.scheduleJob(cronSettings , functionToUpdate)

You seem to use an express endpoint this is ok if you want also to trigger manually .

const functionToUpdate = (req, res){
   // doing something
}


app.get("/fetch",functionToUpdate)

hope it helps

Balint

huangapple
  • 本文由 发表于 2023年6月1日 20:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定