I used to have this code:
static generate() {
var now = new Date();
return {
"date": now.getDate(),
"day": now.getDay(),
"hours": now.getHours(),
"minutes": now.getMinutes(),
"month": now.getMonth(),
"seconds": now.getSeconds(),
"time": now.getTime(),
"timezoneOffset": now.getTimezoneOffset(),
"year": now.getFullYear() - 1900,
};
}
And this worked ok, but took date from the client, which was a bad thing, so I moved this code to a Node server so it looks like this now:
router.get('/generateTime', cors(), (req, res, next) => {
var now = new Date();
res.json({
"date": now.getDate(),
"day": now.getDay(),
"hours": now.getHours(),
"minutes": now.getMinutes(),
"month": now.getMonth(),
"seconds": now.getSeconds(),
"time": now.getTime(),
"timezoneOffset": now.getTimezoneOffset(),
"year": now.getFullYear() - 1900,
});
});
The generate()
function looks like this now:
static async generate() {
var now = await fetch('link/generateTime').catch(e => console.log(e));
now = await now.json();
return now;
}
Before that I was doing some firebase CRUD like this:
export function setLastActiveTime() {
firebase.database().ref('Users').child(firebase.auth().currentUser.uid).child('lastActiveTime').set(Time.generate()).catch(e => console.log(e));
}
So now, I made it work using async await
like this:
export async function setLastActiveTime() {
await firebase.database().ref('Users').child(firebase.auth().currentUser.uid).child('lastActiveTime').set(await Time.generate()).catch(e => console.log(e));
}
But the problem is, I would need to change a lot of code, in other places for this way to work.
Is there a way to keep the old setLastActiveTime()
function as it was, and only change the generate()
function so it grabs the date from the server, and waits until the async call is done?