2017-01-23 208 views
1

我是新來的JavaScript,不能找出如何解決這個問題裏面。我目前正在嘗試創建一個個人推特機器人,它可以在新推文上提醒我,看到新的關注者等。我正在用node.js和npm軟件包Twit編寫這個機器人。我目前正在嘗試創建一個創建新Tweets的函數。我的問題是,如果我嘗試運行我的代碼,我從node.js中得到一個錯誤。如何從另一個函數調用功能相同的類

這裏是我的bot.js文件

var config = require("./app/config"); 
var TwitterBot = require("./app/classes/TwitterBot"); 
var Twit = require("twit"); 

var T = new Twit(config); 
var app = new TwitterBot(T); 

app.tweet({ text: 'This is my test Tweet.' }); 

我的類文件

module.exports = class TwitterBot { 
    constructor(T) { 
    this.T = T; 
    this.colors = require('colors/safe'); 
    console.log("Class works"); 
    } 

    tweet(data) { 
    this.T.post(
     'statuses/update', 
     { status: data.text }, 
     function(err, data, response) { 
     if (err) { 
      this.error("Something went wrong"); 
     } else { 
      this.success("Tweet successfully created"); 
     } 
     } 
    ); 
    } 

    error(something) { 
    console.log("Error: " + something); 
    // This is just an example in my code it performs a switch function 
    } 

    success(something) { 
    console.log("Success: " + something); 
    // This is just an example in my code it performs a switch function 
    } 

} 

我的錯誤:

Class works 
C:\Users\Colin\Desktop\Twitter-Bot\app\classes\TwitterBot.js:14 
      this.error('tweet', err); 
      ^

TypeError: Cannot read property 'error' of undefined 
    at C:\Users\Colin\Desktop\Twitter-Bot\app\classes\TwitterBot.js:14:15 
    at C:\Users\Colin\Desktop\Twitter-Bot\node_modules\twit\lib\twitter.js:118:13 
    at onRequestComplete (C:\Users\Colin\Desktop\Twitter-Bot\node_modules\twit\lib\twitter.js:324:7) 
    at Request.<anonymous> (C:\Users\Colin\Desktop\Twitter-Bot\node_modules\twit\lib\twitter.js:341:7) 
    at emitOne (events.js:101:20) 
    at Request.emit (events.js:188:7) 
    at Gunzip.<anonymous> (C:\Users\Colin\Desktop\Twitter-Bot\node_modules\request\request.js:1001:12) 
    at Gunzip.g (events.js:291:16) 
    at emitNone (events.js:91:20) 
    at Gunzip.emit (events.js:185:7) 
    at endReadableNT (_stream_readable.js:974:12) 
    at _combinedTickCallback (internal/process/next_tick.js:74:11) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 

我的問題是,我不能調用的函數(錯誤或成功)從我的推特功能全部在我的類TwitterBot。我習慣寫我這樣的代碼在PHP

public function tweet($text) { 
    // ... Some function that sends the tweet and returns ($err, $data, $response) 
    if ($err) { 
     // I call other functions inside the same class with $this->functionName(); 
     $this->error($err); 
    } 
    } 

    protected function error($err) { 
    // do something with the $err variable 
    } 

但在JavaScript中它似乎並不喜歡這個工作。有人能幫我解決這個問題嗎?

回答

2

使用箭頭功能。 function() { }表示法的其中一個問題是它將this更改爲窗口對象。如果您使用() => { }相反,this參考應該保持你希望它(例如類)的值。

這裏有arrow functions一組文檔。尤其要注意箭頭函數在處理類變量(如thissuper)方面的參考。

這樣做的唯一可能的缺點是,箭頭功能在預ES6語言環境中工作,所以你可能需要,如果你需要在你的項目中廣泛的瀏覽器支持使用像巴貝爾一個transpiler。鑑於這是一個服務器端應用程序,但我懷疑這會是一個問題。對於快速回答

+0

非常感謝您的快速回答,現在它工作得很好,並且由於瀏覽器支持,這只是一個小個人機器人,只有我將在終端(cmd)中使用。再次感謝,祝你有美好的一天:) – Truzze

+0

@Truzze當然!祝你有個美好的一天,不要忘記接受我的或者我的答案,以便讓人們知道如果他們遇到同樣的問題該怎麼辦! – furkle

1

您可以通過綁定傳遞給this.T.post()或使用箭頭功能的回調函數避免這種情況。將tweet方法更改爲:

tweet(data) { 
    this.T.post(
    'statuses/update', 
    { status: data.text }, 
    (err, data, response) => { // this line got changed 
     if (err) { 
     this.error("Something went wrong"); 
     } else { 
     this.success("Tweet successfully created"); 
     } 
    } 
); 
} 
+0

感謝,這正是我需要和工作完全正常。我選擇接受用戶@furkle的答案,因爲他提供了更多信息,但您的答案也是正確的。再次感謝,祝你有美好的一天。 :) – Truzze

相關問題