SharadCodes SharadCodes - 9 months ago 96
TypeScript Question

self = this but cannot access self.function within a callback

I have the following block of code to store the correct reference to

I am using Mongoose for

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
to be
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
within the
(not callback) is still giving me
is not a function
error. Somehow,
is not referencing the class.
What may be the problem?


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