6

I have the following HttpClient.post call which return an array of Objects.

import { map } from 'rxjs/operators/map';

public getArray(profileId: number): Observable<any> {
    return this.http.post("/api/url", postObject, HttpSettings.GetDefaultHttpRequestOptions())
        .pipe(map(data => { 
          console.log(data); // logs (2) [{…}, {…}]
          return data;
        }));
}

I need to Instantiate an Array of objects. I can't just type assert because I need the Thing constructor to parse some json and other.

Essentially what I want to do is:

.pipe(map(data => { 
      console.log(data); // logs (2) [{…}, {…}]
      return data.map(v => new Thing(v));
    }));

However I can't (to my knowledge) because data is of Type ArrayBuffer and is not iterable. How can I achieve this?

0

1 Answer 1

12

How is the data actually sent from the server? Is it json or something else? If it is json try specifying that in the HttpClient's call (see documentation) using responseType:'json' in the options parameter.

import { map } from 'rxjs/operators/map';

public getArray(profileId: number): Observable<Thing[]> {
    var options = HttpSettings.GetDefaultHttpRequestOptions();
    options.responseType = 'json';

    return this.http.post("/api/url", postObject, options)
        .pipe(map(data => data.map(v => new Thing(v))));
}
Sign up to request clarification or add additional context in comments.

1 Comment

could you please look at stackoverflow.com/questions/51854143/… I think I am facing similar issue but my code compiles under certain conditions and doesn't under some. The issue is around ArrayBuffer but I don't know why it compiles sometimes.

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.