-23

I have a nodejs issue here, and I really don't know why it happens.

Here is my code :

 isInTimeSlot() {
  return new Promise((resolve, reject) => {
   var date = new Date()
      var hour = date.getHours()
      hour = (hour < 10 ? "0" : "") + hour
      var min  = date.getMinutes()
      min = (min < 10 ? "0" : "") + min
      if (hour >= this.followMinHour && hour <= this.followMaxHour) {
       return resolve(42)
      } else if (hour >= this.unfollowMinHour && hour <= this.unfollowMaxHour) {
       return resolve(1337)
      } else {
       return reject()
      }
  })
 }

 checkProjectTimeSlot() {
  return new Promise((resolve, reject) => {
   var timer = setInterval(function() {
    console.log('Checking if bot is in time slot')
    this.isInTimeSlot()
    .then((mode) => {
     clearInterval(timer)
     resolve(mode)
    })
   }, 5000)  
  })
 }

So here are 2 simple methods of my ES6 class, and when I execute it, I have the following error:

this.isInTimeSlot()
                     ^
TypeError: this.isInTimeSlot is not a function 

Can you see the error?

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Alex Trupin
  • 113
  • 2
  • 7
  • 7
    Please find a better title for your question... – Yoshi Sep 02 '16 at 10:10
  • When you're inside your Promise, `this` is no longer referring to what you expect it to. Read [this](http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/) and you will fix it. – byxor Sep 02 '16 at 10:11
  • Probably `this` is referring to a different context than the one you think it refers to. **WTF Alex!** – siannone Sep 02 '16 at 10:11
  • You're not using an arrow function, so the *this* is not *this* you want – Mario Santini Sep 02 '16 at 10:12
  • Apart from what other already told you: 1. `isInTimeSlot` should not be a promise; 2. you'd better user libs like `moment.js` instead of dealing w/ timestamps on your own. – moonwave99 Sep 02 '16 at 10:13

1 Answers1

2

When you're in a promise return, or in a timer, your this changes.

isInTimeSlot() {
    return new Promise((resolve, reject) => {
        var date = new Date()
        var hour = date.getHours()
        hour = (hour < 10 ? "0" : "") + hour
        var min  = date.getMinutes()
        min = (min < 10 ? "0" : "") + min
        if (hour >= this.followMinHour && hour <= this.followMaxHour) {
            return resolve(42)
        } else if (hour >= this.unfollowMinHour && hour <= this.unfollowMaxHour) {
            return resolve(1337)
        } else {
            return reject()
        }
    })
}

checkProjectTimeSlot() {
    var that = this;
    return new Promise((resolve, reject) => {
        var timer = setInterval(function() {
            console.log('Checking if bot is in time slot')
            that.isInTimeSlot()
            .then((mode) => {
                clearInterval(timer)
                resolve(mode)
            })
        }, 5000)        
    })
}
Rajesh
  • 24,354
  • 5
  • 48
  • 79
Kevin Grosgojat
  • 1,386
  • 8
  • 13