1
votes

I am trying to store value from observable to a variable inside the component using a service however it always has the value of undefined. "names" inside subscribe contains value as expected.

this.dataClient.obsNames.subscribe(names => {
      
      this.names = names; //declared above as public names = [];
     
    });
1
how is the variable names defined in component? - Amrit
@amrit it is declared as public names = []; ... i have also tried changing it's access type to private and it still didn't help - caricature
try it defining like names :any - Amrit
why did you decide that this.names is not actually set to obsNames payload? How exactly you checked that? - Sergey P. aka azure
@SergeyP.akaazure i check it using the built-in debugger in chrome and in visual studio code - caricature

1 Answers

2
votes

What i think you have print this.names on console outside of subscribe.which is asynchronous method.due to which your getting undefined.

Try this:

 `this.dataClient.obsNames.subscribe(names =>{ 
      this.names = names;
      console.log(this.names);
  });`