【问题标题】:Calculate time difference within cloud functions计算云函数内的时间差
【发布时间】:2019-08-16 01:45:20
【问题描述】:

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

const p = usersReference.add({ 
    ...,
    registerTime: admin.firestore.FieldValue.serverTimestamp(), 
    ...
});

我的目标是创建另一个云函数,获取用户作为输入并在用户注册后至少 5 天后返回:

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 将被弃用,我不知道如何处理它。

【问题讨论】:

  • 您有一份用户文档吗?
  • 我不明白你问了什么,但是从数据库中获取用户的功能工作正常,数据库中有很多用户要测试,他们都有一个时间戳 registerTime 在他们的文件。
  • 对不起,我不清楚:我想用户有一份文档,其中存储了用户注册的时间戳。这个假设正确吗?
  • 我不确定我是否理解了。有主集合users,然后有很多(随机生成的)id,每个id代表一个用户,包含信息字段{ name (string), ..., registerTime(timestamp) }。
  • 我认为答案是“是”:用户有一份文档。

标签: firebase timestamp google-cloud-functions


【解决方案1】:

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

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

});

如果您确定查询只返回一个文档(根据您问题下的 cmets,这似乎是这种情况),您可以这样做:

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

});

【讨论】:

  • 这The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK的东西有什么意义吗?我经常收到此警告,但似乎毫无意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 2016-04-23
  • 2023-02-13
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
相关资源
最近更新 更多