#Subscribe returning SafeSubscriber instead the updated data from service

34 messages · Page 1 of 1 (latest)

sour isle
#

Service

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable()

export class AdminService {
  private requestDtoSource = new BehaviorSubject<any>({});
  requestDto = this.requestDtoSource.asObservable();

  constructor() { }

  changerequestDto(requestDto: any) {
    this.requestDtoSource.next(requestDto);
  }

}

component where I change the data

    onCompileRequest(request: RequestDto) {
        this.adminservice.changerequestDto(request.idRequest);
    }
    public compileResponse(request: RequestDto) {
        this.onCompileRequest(request);
        
    }

component where I should recieve data

this.adminservice.requestDto.subscribe(data => { this.requestDto = data; }```

return after subscribe: 

SafeSubscriber {initialTeardown: undefined, closed: false, _parentage: null, _finalizers: Array(1), isStopped: false, …} ```

wraith schooner
#

I suspect you're checking the wrong property.
Replace assignment with logging and see what you get:

this.adminservice.requestDto.subscribe(data => console.log(data))
sour isle
#

it returned an empty object

#

new BehaviorSubject<any>({});

#

this basically

wraith schooner
#

Did you emit soething in that subject?

sour isle
#

i m not sure if i understanded the question

wraith schooner
#

If it logs an empty object, and you didn't emit still anything from that subject, it works as expected, and you were checking the wrong property.

sour isle
#

the changeRequestDto works for sure, i checked it with a debugger but i dont why i cannot fetch the data in the other component...

#

im new to angular

wraith schooner
#

Where did you get the line mentioning the SafeSubscriber?

sour isle
#

when i console.log this this.adminservice.requestDto.subscribe(data => { this.requestDto = data; }

wraith schooner
#

Well, that's expected.
As I said earlier, you're checking the wrong property.
What you actually assign to this.requestDto is the value received by that subscriber (data), not the subscriber itself.

sour isle
#

understood

wraith schooner
#

Angular heavily relies on RxJs. Better get the gist of that ASAP.

sour isle
#

but still I didnt get the new value, do you know why?

#

i got basically 0

#

the changeRequestDto works when i debugged it

#

the new data doesent seem to be replaced

wraith schooner
#
changerequestDto(requestDto: any) {
    console.log(requestDto);
    this.requestDtoSource.next(requestDto);
  }

and

this.adminservice.requestDto.subscribe(data => console.log(data))

And copy paste the logging lines you get.

sour isle
#

first line 71 and second line an empty object

wraith schooner
#

Please copy paste. DOn't try to interpret.

sour isle
#

71
{}

#

maybe cuz they ae different types?

worn fern
#

So you probably have several instances of AdminService. The fact that it's not decorated with @Injectable({ providedIn: 'root' }) is already a smell. How and where do you have a provider for that service?

wraith schooner
#

You should post complete classes, not excerpts.

sour isle
#
    selector: 'common-header',
    templateUrl: './header.component.html',
    styleUrls: ['./header.component.scss'],
    providers: [AdminService]
}) ```
#
    selector: 'requests-table',
    templateUrl: './requests-table.component.html',
    styleUrls: ['./requests-table.component.scss'],
    encapsulation: ViewEncapsulation.None,
    providers: [AdminService]
}) ```
worn fern
#

There: each component has its own instance of AdminService, since you have a provider for the service in each component. Don't do that. Use providedIn: 'root', don't define any provider, and you'll have a single instance, that you can inject everywhere.

sour isle
#

hmm ok lemme try

#

thanks a lot

#

Ive got more headaches in 2 weeks of frontend than in 3years of backend lol