0
votes

I need to call/subscribe to the external service observable method and take value from it. I try it, it component constructer but always return undefined. I examine put console.log inside the method the value is there. need some expert helpt to take this object ->string value in angular.

     this.x:string; 
        constructor(
        private readonly socService: ocService) { }
    
        
      ngOnInit(): void {
       
    
        this.ocService.getActiveoc().subscribe(oc => { this.x= oc.id });
        
        //TODO For Testing
        if(this.x){
          console.log("ID" + this.x)
          this.store.dispatch(selectPatientAction({ x: this.x}));
        }
      }

   onScanX() {
    this.store.dispatch(scanX({x: this.x }));
    this.isocSelected = true;
  }
2

2 Answers

1
votes

Everything you want to do with that value should be done in the subscribe method. Dispatch your action in that method too.

I'm assuming you are using NgRedux here.

    constructor(
       private readonly socService: ocService
       private store: NgRedux<AppState>
     ) { }
    
        
      ngOnInit(): void {
        this.ocService.getActiveoc().subscribe(oc => { 
          this.x = oc.id;
          this.store.dispatch(selectPatientAction({ x: this.x}));
        });
      }

   onScanX() {
    this.store.dispatch(scanX({x: this.x }));
    this.isocSelected = true;
  }
1
votes

I think you have a typo in this section?

this.ocService.getActiveoc().subscribe(soc => { this.x= oc.id });

I think it should be soc.id rather than oc.id?