简体   繁体   中英

angular REST get ID only from call

I want to do a call then get the ID from that call and use it in some code later but how do I only get the ServiceProvider number? this is what I get from my rest call:

     {"mitId": 18,
     "ServiceProvider": "2"}

this is my current call

  GetServiceProviderId() {
    var spid = this.http.get<Info>(this.rooturl + 'info', { headers: this.reqHeader})

    return spid;
  }

So I want to use the ServiceProvideNumber in another call but how do I only return the 2?

Return observable from method GetServiceProviderId as:

GetServiceProviderId() {
    return this.http.get<Info>(this.rooturl + 'info', { headers: this.reqHeader});
}

Subscribe to consume value from response wherever you want as:

GetServiceProviderId.subscribe(res => {
    console.log(res);
    //here you will get id
})

I ended up doing this:

  GetServiceProviderId(): Observable<Info> {
    return this.http.get<Info>(this.rooturl + 'info', { headers: this.reqHeader })
  }

this just returns the json request I then will use pipe and flatmap to get the '2'

  GetInstallation(): Observable<Installation[]> {
    return this.GetServiceProviderId().pipe(
      flatMap(info => {
        return this.http.get<Installation[]>
          (this.rooturl +
          "url/?serviceproviderid=" +
          info.ServiceProviderId
      })
    )
  }

this will make the call api/url/?serviceproviderid=2

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