-1
  ngOnInit(): void {
    this.appData$ = this.dataService.getAppData()
  }

  getCustbuttonPressed(): void {
    let id= this.appData$.subscribe(r => r.id)
    const request: CustRequest = {
       name: 'some name',
       id: id // <-- this throws 'Type 'subscribe' cannot assign to type 'string'
    }
    this.service.getcustomer(request).subscribe(response => {console.log(response)})
  }

Basically I want to wait for the value and set it before sending the request. How do I do it correctly?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
logger
  • 1,983
  • 5
  • 31
  • 57
  • Does this answer your question? [How do I return the response from an Observable/http/async call in angular?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular) – Heretic Monkey Jan 27 '21 at 16:04

2 Answers2

0

Just move the things that depend on the id into the subscribe callback:

getCustbuttonPressed(): void {
    this.appData$.subscribe(r => {
        const request: CustRequest = {
           name: 'some name',
           id: r.id 
        }
        this.service.getcustomer(request).subscribe(response => {console.log(response)})
    })
  }
Ramesh Reddy
  • 10,159
  • 3
  • 17
  • 32
0

Maybe try like this :

getCustbuttonPressed(): Observable<any> {
    return this.appData$.pipe(
      map(r => {
        return {
          name: 'some name',
          id: r.id
        } as CustRequest;
      })
      switchMap(request => this.service.getcustomer(request))
    ).subscribe(console.log);
}
Emilien
  • 2,701
  • 4
  • 15
  • 25