-19
我在這裏有一個nodejs問題,我真的不知道它爲什麼會發生。爲什麼我在這裏得到錯誤「不是函數」?
這裏是我的代碼:
\t isInTimeSlot() {
\t \t return new Promise((resolve, reject) => {
\t \t \t var date = new Date()
\t \t var hour = date.getHours()
\t \t hour = (hour < 10 ? "0" : "") + hour
\t \t var min = date.getMinutes()
\t \t min = (min < 10 ? "0" : "") + min
\t \t if (hour >= this.followMinHour && hour <= this.followMaxHour) {
\t \t \t return resolve(42)
\t \t } else if (hour >= this.unfollowMinHour && hour <= this.unfollowMaxHour) {
\t \t \t return resolve(1337)
\t \t } else {
\t \t \t return reject()
\t \t }
\t \t })
\t }
\t checkProjectTimeSlot() {
\t \t return new Promise((resolve, reject) => {
\t \t \t var timer = setInterval(function() {
\t \t \t \t console.log('Checking if bot is in time slot')
\t \t \t \t this.isInTimeSlot()
\t \t \t \t .then((mode) => {
\t \t \t \t \t clearInterval(timer)
\t \t \t \t \t resolve(mode)
\t \t \t \t })
\t \t \t }, 5000) \t \t
\t \t })
\t }
因此,這裏有2種我ES6類的簡單方法,當我執行它,我有以下錯誤:
this.isInTimeSlot()
^
TypeError: this.isInTimeSlot is not a function
你能看到錯誤嗎?
請爲您的問題找到一個更好的標題... – Yoshi
當您進入您的承諾時,'this'不再指您期望的內容。閱讀[這](http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/),你會解決它。 – byxor
也許'this'指的是與你認爲的不同的上下文。 ** WTF亞歷克斯!** – siannone