Home > Blockchain >  How to access id from this httpresponse in angular to perform edit and delete actions based on id
How to access id from this httpresponse in angular to perform edit and delete actions based on id

Time:04-06

In the screenshot showing list of applications created

function to get list of apps -> ts file

 getcmsApps() {
    this.getCmsService.getApps().subscribe((res: any) => {
      this.data = res;
      console.log(res)
    })
  }
  editAppById(appid: string): void {

  }
  deleteAppId(appid: string){

  }

CodePudding user response:

The response you get is an object array. So you can just loop trough the array and access the field by its key name

let AngularResponse = [
{
  createdon: "4/1/2022",
  division: "test",
  email: "[email protected]",
  fax: "1",
  hrbinformation: "1",
  id: "d7ab1fc0-0e33-4fb7-ac71-1bf48ddd8bf8"
},
{
  createdon: "6/2/2022",
  division: "test",
  email: "[email protected]",
  fax: "2",
  hrbinformation: "2",
  id: "a869fdd8-bf99-49d9-bb72-7bb11e6fadbf"
},
]

console.log(AngularResponse[0].id);

//OR to get all of them

AngularResponse.forEach(arrayItem => console.log(arrayItem.id))

CodePudding user response:

Usually the httpService will return an Observable on which you can subscribe in the subscription part you can perform all sorts of operations based on the response you got.

I have to take a look at you code to give you further informations but what I can suggest to do is the following

request = // your http request observable from httpService 
request.subscribe((response) => {
   // retrieve the id from the response object (as I can see in the 
   // screenshot your response is an array so you will probably have to loop 
   // through it)    
});

Another option to consider would be to use RxJs operators to make operations on the response object (eventually using the tap operator) before the subscription part.

  • Related