-1

I know the answer must be silly for you but I have just started javascript to do functions with firebase and I block on my script even after reading several questions with the same problem here (stackoverflow).

When running my script tells me:

TypeError: d.getFullYear is not a function
at Date.sameDay (/user_code/index.js:36:41)
at /user_code/index.js:61:33
at process._tickDomainCallback (internal/process/next_tick.js:129:7)

"

exports.sendNotificationToNewUser = functions.https.onRequest((request, response) => {

    // Enable logging
    // admin.database.enableLogging(true);

    Date.prototype.sameDay = function(d) {
        return this.getFullYear() === d.getFullYear()
        && this.getDate() === d.getDate()
        && this.getMonth() === d.getMonth();
    }

    // Loop through users in order with the forEach() method. The callback
    // provided to forEach() will be called synchronously with a DataSnapshot
    // for each child:
    var query = admin.database().ref("users").orderByKey();
    var defaultAuth = admin.auth();
    query.once("value")
    .then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            // user will be "ada" the first time and "alan" the second time
            var user_id = childSnapshot.key;
            // childData will be the actual contents of the child
            var user_data = childSnapshot.val();

            console.log(user_id);

            admin.auth().getUser(user_id)
            .then(function(userRecord) {
                var create_date = userRecord.metadata.createdAt
                console.log("Creation date:", create_date);
                create_date.setDate(create_date.getDate()+4);
                if (create_date.sameDay(Date.now())) {
                    //things
                }
            });
        });
    });
    response.send("OK");
})
filol
  • 1,669
  • 1
  • 15
  • 38
  • @Satpal yes `created_date` = 2017-03-28T19:02:27.000Z in my test – filol Apr 01 '17 at 13:00
  • @user2263572 i return nothing, this method is include in javascript (i think) – filol Apr 01 '17 at 13:03
  • @Satpal it's not working : `TypeError: (intermediate value).setDate(...).sameDay is not a function` – filol Apr 01 '17 at 13:07
  • 1
    [`Date.setDate()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate): _"Return value: The number of milliseconds between 1 January 1970 00:00:00 UTC and the given date (the Date object is also changed in place)"_ – Andreas Apr 01 '17 at 13:10
  • @Andreas `and the given date`, so it's good no ? – filol Apr 01 '17 at 13:19
  • But it returns a number and not a date object! Should `create_date` be changed at all? – Andreas Apr 01 '17 at 13:30
  • @Andreas If I look at the stackoverflow issues like [this](http://stackoverflow.com/questions/9989382/add-1-to-current-date), my method is good, right? – filol Apr 01 '17 at 13:37
  • @Andreas i have update my code, i don't have the same error – filol Apr 01 '17 at 13:44
  • Please have a look at the documentation I've linked in my first comment. There are all functions explained (parameter, what they do, **return values**, ...). [`.now()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) also returns the number of milliseconds between 1 January 1970 00:00:00 UTC and the given date – Andreas Apr 01 '17 at 13:50
  • yes, thx, i have already fix this problem, thank you for you help ! if can you post one answer with the doc, i can accept – filol Apr 01 '17 at 13:51

1 Answers1

0

The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

Here you're calling .sameDay() with a number

if (create_date.sameDay(Date.now())) {
    //things
}

But your function expects a date object. Change Date.now() to new Date()

if (create_date.sameDay(new Date())) {
    //things
}
Andreas
  • 21,535
  • 7
  • 47
  • 56