0

I have been trying to get data from my webservice into an array of my custom type (both models match, I have checked that)

The object itself people is defined and initialised as a black array of type Person.

However when making the subscribe call the array is now undefined.

This was working, I'm not actually sure why it stopped

Declaration

  people: Person[] = [];

Subscription

getPeople() {
    this.isLoading$.next(true);
    this.PeopleService.GetAll().subscribe(
      o => {
        if (o) {
          this.people = o;
        }
        else {
          this.people = [];
        }
      },
      error => {
        console.log(error);
      },
      () => {
        this.isLoading$.next(false);
      }
    );
  }

Before the method, this.people is defined as Array[0]

When in the getPeople method, this.people is undefined. o is defined and has data.

UPDATE 1 10:41 2019/05/03

I noticed the following when debugging

Console

could this be the cause, if so why? I've been writing all my methods like this and none have ever given me this issue

5
  • 1
    how do you invoke the getPeople method? Commented May 3, 2019 at 8:09
  • on the constructor I call this.getPeople() Commented May 3, 2019 at 8:15
  • Try this, In case of error, put this.people = []; also. the problem is, in error section you are not handling error. make sure that the api call is working currectly Commented May 3, 2019 at 8:28
  • @Darkestlyrics please provide enough code so that the problem was more apparent. At the moment what you claim - is impossible Commented May 3, 2019 at 8:47
  • @zerkms I seem to have stumbled onto why, posting the answer now. Commented May 3, 2019 at 8:50

2 Answers 2

2

My guess is you are using the debugger/breakpoints inside your IDE to check the value. I also guess you are compiling your typescript to es5. Another guess is that the sourcemaps are not loading inside your debugger environment.

All this combined makes a nice incoherent mess for you.

What typescript does under the hood with the arrow function notation () => {}, is set a _this = this before the arrow, and change the function to function() {}, which will have a new this context. This happens when you compile to a version that does not support the arrow notation.

If you actually do console.log(this.people) instead of setting a breakpoint, it will output the correct/expected value. This is compiled to console.log(_this.people) to the this of the outer scope. Does that make sense?

Anyways, you should find a way to have your IDE load the sourcemaps, or use the chrome debugger which does load the correct sourcemaps, and give you a more concise picture of your code

Sign up to request clarification or add additional context in comments.

1 Comment

That is interesting, I'll keep that in mind. Thanks for the explanation.
0

I noticed that this section

        o => {
        if (o) {
          this.people = o;
        }
        else {
          this.people = [];
        } 

Changes this to be of type SafeSubscriber instead of the actual Component. So by all rights, this.people IS undefined in that scope.

Changing it to

  o => (this.people = o)

works as expected

I'm not entirely sure why and if anyone can figure that out I'll gladly mark it as the accepted answer.

2 Comments

That code cannot change this, it's impossible. Your solution is semantically identical and cannot change the observed behaviour.
@zerkms PierreDuc gives a nice explanation as to what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.