RxDev
RxDev

Reputation: 295

Angular How to get access to data from service after subscription

this my service

getDataDetails(id: any) {
     this.dataDocumment = this.afs.doc('data/' + id);
     return this.data = this.dataDocumment.valueChanges().subscribe(res =>{
       this.data = res;
       console.log(this.data);
     });
   }

and this is my component where I wish to fectch the data

ngOnInit() {
    this.id = this.route.snapshot.params['id'];
    this.dataD = this.dataService.getDataDetails(this.id);
    console.log(this.dataD);
  }

The data in display of the service display well in the console

{adresse: "Rue de France #10", bureau: "78", centre: "Lycée Français", cin: "751-02-785-782-002", commune: "Paris", …}

but it display this message from my component

Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null, …}

PS : dataD is type "any"

I correctly do the subscripe in the service whyi is is dsplaying this in my component ?

Upvotes: 0

Views: 989

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41445

return the observable from the service and subscribe it inside the component.

getDataDetails(id: any) {
    this.dataDocumment = this.afs.doc('data/' + id);
    return this.dataDocumment.valueChanges()
}
ngOnInit() {
    this.id = this.route.snapshot.params['id'];
    this.dataService.getDataDetails(this.id).subscribe(
    res => {
        this.dataD = res;
        console.log(this.dataD);
    });
}

Upvotes: 1

Related Questions