0

Upgrading a project from Angular 4 to 6 and having some trouble getting my return to work. My json comes in like

{
    "incomingViolationList":[{"productName": "Mirror",…],
    "incomingViolationListCount": 67
}

My service call use to look like this but in A6 .map no longer works.

return this.http.get('MappViolations/MappViolations?', options)
      .map(response => <GridDataResult>{
        data: response.json().incomingViolationList,
        total: response.json().incomingViolationListCount
      });

I have starting my new service call but am at a loss how to seperate into "data" and "total"

return this.http.get<IncommingViolations[]>
      (AppSettings.API_ENDPOINT + 'MappViolations/MappViolations?', { params });
4
  • But what is happening? Commented Nov 7, 2018 at 18:33
  • in the A4 version .map is no longer a option Commented Nov 7, 2018 at 18:33
  • 2
    use pipe and then map. Commented Nov 7, 2018 at 18:34
  • Perhaps helpfully, read github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md It comes with a change in rxjs, where chained dot-operators were replaced with pipe/taps. Commented Nov 7, 2018 at 18:38

3 Answers 3

2

In Angular 6, you will be using HttpClient which returns the json response by default. So you can remove json from the response.

return this.http.get('MappViolations/MappViolations?', options)
     .pipe( 
 map(response => ({
  data: response.incomingViolationList, //<-- remove json
  total: response..incomingViolationListCount //<-- remove json
 })

);

Sign up to request clarification or add additional context in comments.

1 Comment

I am using httpclient. But this is the A4 code. How do I translate from .map down into A6?
2

In Angular 6 there is HttpClient used instead of Http service

Using HttpClient the response object is a JSON by default, so there's no need to parse it anymore (response.json() is not needed)

Also if you update RxJS 6 with Angular 6 update it will look something like below pipeable operators.

return this.http.get('MappViolations/MappViolations?', options)
  .pipe( 
     map((response: any) => ({
      data: response.incomingViolationList,
      total: response..incomingViolationListCount
     })
  );

2 Comments

ok, I think this is almost there. I am getting the following error. [ts] Property 'incomingViolationList' does not exist on type 'Object'.
or use map((response: {incomingViolationList: any, incomingViolationListCount: any}) => ({
-1

So you have stated in the comment that your case is that map is not an option. Well actually it still is, but it is used slightly different way

return this.http.get('MappViolations/MappViolations?', options)
      .pipe(
        map(response => <GridDataResult>{
          data: response.json().incomingViolationList,
          total: response.json().incomingViolationListCount
      }));

Notice usage of pipe directly on observable insteed of map.

Comments

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.