0

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.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Daniel
  • 7,357
  • 7
  • 32
  • 84
  • Do you have one document by user? – Renaud Tarnec Mar 25 '19 at 18:23
  • I didn't understand what you asked, but the function to get a user from the database works fine, there are a lot of users in the database to test and they all have a timestamp registerTime in their documents. – Daniel Mar 25 '19 at 18:24
  • Sorry I was not clear: I imagine that there is one document by user where you store the timestamp of the user's registration. Is this assumption corrrect? – Renaud Tarnec Mar 25 '19 at 18:26
  • I'm not sure I understood yet. There is the main collection `users`, then there are a lot of (randomly generated) ids and each of these ids represent an user, which contains information fields `{ name (string), ..., registerTime(timestamp) }`. – Daniel Mar 25 '19 at 18:29
  • I think the answer is then "yes": there is one document by user. – Renaud Tarnec Mar 25 '19 at 18:37

1 Answers1

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

});
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Does this `The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK` thing have any meaning? I get this warning a lot but it seems pointless. – Daniel Mar 25 '19 at 18:34
  • Have a look at https://stackoverflow.com/questions/51479679/firebase-update-error-the-behavior-for-date-objects-stored-in-firestore-is-goin – Renaud Tarnec Mar 25 '19 at 18:39