Angular retrieves only the ID from REST call

I need to extract the ServiceProvider number from a REST call response and use it in my code. This is the current output of the call:

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

This is my current function:

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

    return spid;
  }

How can I specifically retrieve only the ServiceProvideNumber, which is "2", for use in another call?

Answer №1

Obtain an observable by calling the method GetServiceProviderId like so:

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

To receive and process the response value, you can subscribe to it at any desired location in your code:

GetServiceProviderId.subscribe(res => {
    console.log(res);
    //The id will be displayed here
})

Answer №2

After some consideration, I came up with the following solution:

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

This method simply retrieves the JSON request which I then manipulate using pipe and flatMap to extract the '2'.

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

As a result, an API call will be made to api/url/?serviceproviderid=2

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to setting a default value for a select option in Angular 2+

I am having trouble setting a default option in my select box using the html selected property. I want the selected option to be assigned to StartingYear, but currently it's not working as expected. <select [(ngModel)]="StartingYear"> < ...

Updating Azure Table Storage Record through RESTful Web API调用

Currently, I am in the process of testing Azure Table Storage's REST API services. While I can successfully retrieve data from the table, I am encountering difficulties when attempting to update records using JavaScript. Interestingly, performing the ...

Tips on mocking Angular HttpClient post request in Karma unit test

I am working on a component that has the following code in its query() function. I am looking for a way to mock the results returned by the http post. How can I achieve this goal? query(){ Observable.forkJoin( this.http.post<any[]>(URL, ...

Creating a Jsonifiable type that aligns with static interfaces: A step-by-step guide

When working with Typescript, there are 3 types that share similarities as they are composed of primitive types, maps, and arrays: type Color1 = { [component: string]: number } type Color2 = { green: number red: number blue: number } interface Colo ...

Guide to Authenticating API with an XAPP Token in PHP

Can you assist me with translating this code into PHP within my script? curl -v -X POST "https://api.artsy.net/api/tokens/xapp_token?client_id=...&client_secret=..." I possess both the client_id and client_secret. After acquiring the token, how can ...

What is the method for inputting multi-line strings in the REST Client extension for Visual Studio Code?

Clarification There seems to be some confusion regarding the nature of the data I am storing. I am developing a code snippet web application using Express.js and MongoDB. The purpose is not to store executable code for later use; instead, I am saving snipp ...

The Angular/fire docData feature functions properly on Chrome and Safari on macOS, however, it fails to emit on Capacitor

Issue with angular/fire method docData in capacitor-ios I am encountering a problem where the docData method works perfectly on desktop browsers like Chrome and Safari MacOS, but fails to emit updates on capacitor-ios (which uses webkit-safari on iOS). T ...

Including a hyperlink in a table using Angular 8

I have a table with multiple columns, one of which is the "documents" column that contains downloadable files. How can I make just the document name clickable as a link? HTML <p-table #dt3 [columns]="colsPermessi" [value]="permessi" [paginator]="true" ...

Difficulty encountered while utilizing the useState hook with a complex object in ReactJS

In my function component, I am using the useState hook to declare a state for a complex object as follows: const [order, setOrder] = useState<IMasterState>({ DataInterface: null, ErrorMsg: "", IsRetrieving: true, ...

There are no call signatures available for the unspecified type when attempting to extract callable keys from a union

When attempting to write a legacy function within our codebase that invokes methods on certain objects while also handling errors, I encountered difficulty involving the accuracy of the return type. The existing solution outlined below is effective at cons ...

Having trouble with the input search function displaying the wrong date on the material date picker? You may need to convert it

I have customized the MomentDateAdapter to meet my specific requirements. Interestingly, when I choose a date from the calendar, the output is accurate. However, if I enter a date manually in the input field, the output is incorrect. When dealing with the ...

Is there a way to display the value of a script function within an HTML block in an Angular application?

I need assistance displaying the "timeStops" value in an HTML block using a script. The "timeStops" output is in array format and I would like to showcase this array value within an HTML block. Here is the output of timeStops: [ "21:00", " ...

One typical approach in React/JavaScript for monitoring the runtime of every function within a program

Experimenting with different techniques such as performance.now() or new Date().getTime() has been done in order to monitor the processing time of every function/method. However, specifying these methods within each function for time calculation purposes h ...

Are additional Angular tags causing issues with the HTML layout in development mode?

When working with templates, I often find myself inserting two components inside the template of another one. Take for example this code snippet from my.component.html: // my.component.html <div class="container"> <div class="row& ...

invoke the next function a different privateFunction within rxjs

I'm trying to figure out how to pass the resetPassword data to the _confirmToUnlock method in Typescript/RxJS. Here is my subscribe method: public invokeUnlockModal() { let resetPassword = { userName: this.user?.userName}; //i need to send this ...

I'm experiencing a strange issue where my React component renders twice in production mode, despite having strict mode turned off. Can anyone advise me on

Within my layout.tsx, I have a structure that encloses the page with a container div and introduces a separately defined TopBar component alongside. The functionality seems fine, but an issue arises where the component is created before the {children}, as ...

Could not locate the term 'group member'

I am currently working on developing a moderation bot using Discord.js v13. In order to implement functionalities such as mute, kick, warn, and ban commands, I need to verify user permissions. However, I am encountering an issue with my code where it fails ...

Encountering issues with Next.js 13 middleware functionality

On several pages of my website, I require the user to be logged in. To achieve this, I have implemented middleware to check if the user is authenticated and blocked access to certain pages until the user logs in. Below is the code snippet from my file: exp ...

Developing the latest version of ngx-charts

I'm currently working on adding a personalized feature to ngx-charts that I would like to include in a release version. I was successful in implementing it using the standard src directory, but now I want to build a release version for potential distr ...

Chokidar encountered an error on drive C:: EBUSY error code, file 'C:DumpStack.log.tmp' is busy or locked

Encountering an issue while using Angular and running ng serve. The error message displayed is: Error from chokidar (C:\): Error: EBUSY: resource busy or locked, lstat 'C:\DumpStack.log.tmp' Error from chokidar (C:\): Error: EBUSY ...