问题
In my Cloud Firestore database, always a user is registered, the database stores the time the event occurs:
const p = usersReference.add({
...,
registerTime: admin.firestore.FieldValue.serverTimestamp(),
...
});
My goal is to create another cloud function that gets a user as input and returns if there is at least 5 days since the user was registered:
export const get_user_timeleft = functions.https.onRequest((request, response) => {
//useless part of the function...
querySnapshot.forEach(function (documentSnapshot) {
//Here I know that documentSnapshot.data().registerTime
//returns whatever is saved in the database.
//How can I return true or false if there's been 5
//or more days since the user registered?
response.json(TRUE of FALSE);
})
}
})
.catch(function(error) {
response.json(error);
});
});
Clearly I can call admin.firestore.FieldValue.serverTimestamp()
again and try to subtract them, but I don't even know the type it is. In the past I've managed to use is as a Date
, but since Firebase is saying that Date
will be deprecated, I don't know how to deal with it.
回答1:
If you want, in your Cloud Function, to check for each document returned by the querySnapshot.forEach()
loop if there is at least 5 days since the user was registered (i.e. today - registerTime > 5
), you could do as follows:
export const get_user_timeleft = functions.https.onRequest((request, response) => {
//useless part of the function...
const nowDate = new Date();
querySnapshot.forEach(function (documentSnapshot) {
//Here I know that documentSnapshot.data().registerTime
//returns whatever is saved in the database.
//How can I return true or false if there's been 5
//or more days since the user registered?
const elapsedTime = (nowDate.getTime() - documentSnapshot.data().registerTime.getTime());
const daysDifference = Math.floor(elapsedTime/1000/60/60/24);
//Check that the difference is more than 5 and then act accordingly
//It is not crystal clear from your question if there is only one document in the querySnapshot
})
})
.catch(function(error) {
response.json(error);
});
});
In case you know for sure that there is only one document returned by the query (which seems to be the case, in the light of the comments under your question), you could do:
export const get_user_timeleft = functions.https.onRequest((request, response) => {
//useless part of the function...
const nowDate = new Date();
let daysDifference;
querySnapshot.forEach(function (documentSnapshot) {
//Here I know that documentSnapshot.data().registerTime
//returns whatever is saved in the database.
//How can I return true or false if there's been 5
//or more days since the user registered?
const elapsedTime = (nowDate.getTime() - documentSnapshot.data().registerTime.getTime());
daysDifference = Math.floor(elapsedTime/1000/60/60/24);
});
if (daysDifference > 5) {
response.send({ result: true });
} else {
response.send({ result: false });
}
})
.catch(function(error) {
response.json(error);
});
});
来源:https://stackoverflow.com/questions/55344218/calculate-time-difference-within-cloud-functions