Why does `this` change to SafeSubscriber?

I need to find out why my this object changes from view controller to SafeSubscriber in my TS & ionic app.

My data service request:

getStationData(name:string){

        let myURL = "http://some/url/request.php?name="+ name;

        return this.http.get(myURL)
                .map(
                    res => {return this.parseData(res._body);},
                    err => console.log(err));
};

My view controller:

//here, this is still the correct object : view controller
this._dataService.getStationData(name).subscribe((data) =>{
    //here, this now becomes of type safe subscriber?
    console.log(this);
    //when I hover in chrome over this, it shows all view controller parameters.
    //but when I call a method, it's not defined?
});

Is there something wrong with the http.get request?

Is this more of a “this code doesn’t work”, or a conceptual “why does this happen” question?

(I’ve not examined this in recent memory)

I think you’ve accidentally modified your code for posting purposes and erased the problem, but the answer is “because JavaScript is a horrible, very bad, no good language”.

Don’t ever type the word “function”, and don’t ever pass object methods as arguments, and you will avoid 95% of these sorts of bugs.

More of the latter, why does this happen?

Hi

I don’t understand your usage of map as operator following thr get, in combination with an err. I use map with one argument and use .catch to get errors. And not sure if parseData is the recommended way to convert a Response object to Json etc.

And then the subscriber gets real data.

So my view:incorrect usage of rxjs operstors.

Many good examples available on the web. Try angular.io or google “http angular example”

Regards

Tom

1 Like

I am re-writing my app to pure TS now. The interesting thing about my old code is that it used to work with ionic2, now ionic3 fails