1

I'm having a problem with the subscribe method, I'm receving the data, when i put a console.log to see whats in that variable, it works but if I do the same outside that method it says undefined, any ideas why does this happend?

planificadorD: any;

 constructor(private auth: AuthService, private toastService: ToastService,
                private router: Router, private planificadorService: PlanificadorService) {
    }

 ngOnInit() {

    if (this.postData) {
        this.planificadorService.planificadorData(this.postData).subscribe((res: any) => {
            this.planificadorD = res.planificadorData;
            console.log(this.planificadorD); //this works
            });
        }
       console.log(this.planificadorD); //this does not ->undefined
    }

My service

  planificadorData(postData: any): Observable<any> {
    return this.httpService.post('planificador_get', postData);
  }
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Christian
  • 481
  • 1
  • 7
  • 24
  • Or the more general https://stackoverflow.com/q/14220321/3001761 – jonrsharpe Jan 25 '20 at 16:23
  • @jonrsharpe Ok i understand all must be inside the subscribe method, but how do I make it accesible to outside? for example what if I want to sent it to another page? I'm still getting undefined... – Christian Jan 25 '20 at 16:44
  • Other components can subscribe to the same observable from the service. I'd recommend running through https://angular.io/tutorial, which covers some of these ideas. I also blogged about one approach to sharing data a while ago: https://blog.jonrshar.pe/2017/Apr/09/async-angular-data.html – jonrsharpe Jan 25 '20 at 16:46

0 Answers0