0

Im trying to call a method from an another method, and the error i get is this:

TypeError: this.displayCur is not a function

The error is inside the method this.start

my code:

function Game(room , author) {
this.room = room;
this.author = author;
this.wrongLetters = [];
this.rightLetters = [];
this.guessesLeft = 5;
this.correctGuesses = [];
this.wrongGuesses = [];

this.start = function() {
    dClient.on("message" , function(message) {
        //this.prototype.guess(message);
        if (!message.author.bot && this.guessesLeft > 0) {
            console.log("Guess made: " + message);
        } else if (!message.author.bot && this.guessesLeft <= 0) {
            this.end();
        }
        this.displayCur("hello"); < ------ the error occurs here
    });
};


this.displayCur = function(state) {
    this.finStr = "";
    for (var l = 0; l < this.word.length;l++) {
        if (this.correctGuesses.includes(this.word[l])) {
            this.finStr += this.word[l] + "\ \\";   
        } else {
            this.finStr += "_" + "\ \\";
        }

    }
    if (state != "begin") {
        this.room.send(this.finStr);
    }
};
this.displayCur("begin");

}

NOTE

i removed some of the code so it's not filled with unrelated code. so if you see some things that have no context, just ignore them please.

greuzr
  • 3
  • 2

1 Answers1

0

Change

dClient.on("message" , function(message) {

To

dClient.on("message" , (message) => {

so now the this inside of this function still points to your object and not to dClient.

Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120