Calculate time difference within cloud functions

无人久伴 提交于 2021-01-29 06:12:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!