[Solved] Why am I getting the error “is not a function” here? [duplicate]


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

solved Why am I getting the error “is not a function” here? [duplicate]