计算云函数中的时差 [英] Calculate time difference within cloud functions

查看:43
本文介绍了计算云函数中的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Cloud Firestore数据库中,始终注册一个用户,该数据库存储事件发生的时间:

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(), 
    ...
});

我的目标是创建另一个云功能,该功能将用户作为输入并在用户注册后至少有5天时返回:

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);
    });

});


很明显,我可以再次调用 admin.firestore.FieldValue.serverTimestamp()并尝试减去它们,但我什至不知道它的类型.过去,我设法将其用作 Date ,但是由于Firebase表示将弃用 Date ,所以我不知道该如何处理.


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.

推荐答案

如果要在Cloud Function中检查是否存在 querySnapshot.forEach()循环返回的每个文档.用户注册至少5天后(即 today-registerTime> 5 ),您可以执行以下操作:

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);
    });

});

这篇关于计算云函数中的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆