2

This is the response I am passing from the Service to the Component where i should display each value to the UI. I need to parse each value and store to a variable.

[{"profileId":"000234","profileName":"kofi prfl","regionName":"NA  ","fileType":"GRRCN","fileVersion":"1.01","fileFreq":"D01","fileFormat":"FIX","emptyFile":"N","cardMask":"Y","uprInd":"N","dataLevel":"01"}]

this.profileDetailsService.getProfileDetails(this.idDisplay)
      .subscribe(profileResponse => {
          // Should parse the profileResponse here....
          this.searchResult = profileResponse;
          this.spinnerService.hide();
        },
      error => {
          this.spinnerService.hide();
          this.showError();
        }
      );

How to separate profileId, profileName, regionName etc., values?

1
  • Any chance you can post more code? Can I see your service, template and component? Commented Jun 22, 2017 at 1:00

2 Answers 2

2

instead of in your code:

...
this.searchResult = profileResponse;
...

you can use:

searchResult: any[];

...
this.searchResult = profileResponse.json() || {}
...

then you can access each property via searchResult item:

this.searchResult[0].profileId, etc
Sign up to request clarification or add additional context in comments.

1 Comment

That worked like a charm! Was struggling for almost 8 hours with this. Thank you!
0

Update based on comment code:

The reason you're not able to access the properties of searchResult is that the service is returning a response rather than a Javascript object with properties.

From the HTTP Angular docs here:

The response data are in JSON string form. The app must parse that string into JavaScript objects by calling response.json().

I suggest parsing the response in the service to keep things related to the http request there and prevent code duplication.

Change:

return this.http.post(this._url, body, options).map((response: Response) => {
    return response;
}).catch((error) => {
    return Observable.throw(error);
});

To this (if indeed you only want the first profile in the array):

 return this.http.post(this._url, body, options).map((response: Response) => {
        return response.json()[0];
    }).catch((error) => {
        return Observable.throw(error);
    });

The new code parses the JSON object and extracts the first object from the array with .json()[0]. Then rename profileResponse to profileDetails and access this.searchResult's properties using dot notation:

e.g. From another function in the component: console.log(this.searchResult.profileName)

e.g. From your template: {{searchResult.profileName}}

4 Comments

profileResponse.profileId doesn't display the value in the field. It just shows undefined in the console.
Are you sure you're returning the actual object and not a json string from your service? Does your service call response.json()?
return this.http.post(this._url, body, options) .map((response:Response) => { return response; }) .catch((error) => { return Observable.throw(error); }); this.profileDetailsService.getProfileDetails(this.idDisplay) .subscribe(profileResponse => { this.value1 = profileResponse.profileId; this.value2 = profileResponse.profileName; this.searchResult = profileResponse; console.log("stringify values is "+ JSON.stringify(this.searchResult)); this.spinnerService.hide(); } );
this is how i am passing the response from service to component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.