简体   繁体   中英

Angular/rxjs6 - GET before PUT

I would GET fresh data from DB before I PUT (edit) some record.

Service:

getVotes(): Observable<Vote> {
  return this._http.get<Vote>(url);
}

putVote(choose: string){
 this.getVotes().subscribe(
  result => {
    ...
    return this._http.put<Vote>(url + object).subscribe();
  }
 )
}

Now it's working but I think putVote() should return Observable<Vote> so how to return in from internal getVotes() ?

You should map the response from getVote() using switchMap and Remove subscribe from http.put() to return Observable from putVote()

putVote(choose: string){
 return this.getVotes().pipe(switchMap(
  result => {
    ...
    return this._http.put<Vote>(url + object);
  }
 ))
}

Could you try the following code?

getVotes(): Observable<Vote> {
  return this._http.get<Vote>(url);
}

putVote(choose: string): Observable<Vote> {
 this.getVotes().subscribe(
  result => {
    ...
    return this._http.put<Vote>(url + object).subscribe();
  }
 )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM