0

I have the following block of code to store the correct reference to this. I am using Mongoose for Models.Image.

class some {
    public one() {
        var self = this;
        this.another(); //edited: this.another() is not a function
        Models.Image.findById(id).then(function(data) {
            self.another(); //self.another is not a function
            ....
        });
    }
    public another() {
        ....
    }
}

The linter is showing self to be this but when I execute it, gives me error. What is happening here? How to solve it?

The class is a route handler bound to express routes.

I would like to add more. Calling this.another() within the one() (not callback) is still giving me is not a function error. Somehow, this is not referencing the class. What may be the problem?

  • 2
    It's hard to understand what the problem is with the limited code you included. It looks like it should be ok, but since there's a problem it will be best if you included more of your code. On a side note, you don't even need this `self = this` as you can just use an arrow function. – Nitzan Tomer Sep 01 '16 at 12:38
  • 5
    How are you calling `one()`? – JohnnyHK Sep 01 '16 at 12:38
  • I have updated the problem. –  Sep 01 '16 at 12:42
  • 1
    Have you tried using an arrow function `() => {}` instead? This way there is no need for the assignedment of `this` to `self`. – Sebastian Sebald Sep 01 '16 at 12:55
  • It's highly likely that you are specifying `some.one` as some kind of callback, which is missing context information, as suggested to by @JohnnyHK's comment. You would need to change that to `() => some.one()` or `some.one.bind(some)`. –  Sep 01 '16 at 13:21

2 Answers2

4

Edited: The class is a route handler bound to express routes.

Probably the problem is here.

Because of how the method one is called, this is not bound to an instance of the class some. The issue is not about the callback. At the first line of one, this already has a wrong value:

var self = this; // self has a wrong value because this has a wrong value 
Paleo
  • 21,831
  • 4
  • 65
  • 76
  • Yeah, the problem was with the way route handler was bound. I had used `some.one.bind(some.one)`. I replaced it to be `some.one.bind(some)`. Thank you. After I knew where the problem was, the solution was here http://stackoverflow.com/questions/15604848/express-js-this-undefined-after-routing-with-app-get –  Sep 01 '16 at 13:24
  • Here ist some MDN docu of bind, including a great example: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Function/bind – Michael Biermann Oct 06 '19 at 00:28
-3

another() is expected to be within public one() :

class some {
    public one() {
        var self = this;
        Models.Image.findById(id).then(function(data) {
            self.another(); //self.another is not a function

            ....
        });
    }
    public another() {
        ....
    }
}

Maybe better like this:

class some {
    public one() {
        var self = this;
        Models.Image.findById(id).then(function(data) {
            self.another(); //self.another is not a function
            ....
        });
        another() {
            ....
        }
    }
}
Marco V
  • 2,553
  • 8
  • 36
  • 58