How do I pipe/map to return an array in Angular 7









up vote
0
down vote

favorite












When I make a GET call via HttpClient I want to pass back an array of the actual typed objects, vs. just a generic 'object', and so I tried to do this:



getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
return this.http
.get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
.pipe(
map(x =>
const ret = EhsAssessmentAr.fromJson(x);
ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
return ret;
)
)



That fromJson method is declared to return the proper class, and I thought since I was sending this through map that I'd get back an array. I'm REALLY new to RxJs so I'm sure I'm just doing something completely stupid there.



I'm getting theerror:




TS2322: Type 'Observable<EhsAssessmentAr>' is not assignable to type 'Observable<EhsAssessmentAr>`











share|improve this question





















  • Possible duplicate of Observable<> not assignable to type Observable<SomeType>
    – paulsm4
    Nov 10 at 1:17










  • Doesn’t seem the same to me. I declared as an array return type and I feel like I am returning an array type. What you pointed at was returning array or object.
    – Gargoyle
    Nov 10 at 1:24










  • did you check stackoverflow.com/a/43233448/4591364 ?
    – Microsmsm
    Nov 10 at 1:37










  • were you be able to fix the problem?
    – Microsmsm
    Nov 10 at 2:27










  • Not at my computer but I think the issue is the map I used takes the array not a single object like the “normal” map
    – Gargoyle
    Nov 10 at 2:28














up vote
0
down vote

favorite












When I make a GET call via HttpClient I want to pass back an array of the actual typed objects, vs. just a generic 'object', and so I tried to do this:



getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
return this.http
.get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
.pipe(
map(x =>
const ret = EhsAssessmentAr.fromJson(x);
ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
return ret;
)
)



That fromJson method is declared to return the proper class, and I thought since I was sending this through map that I'd get back an array. I'm REALLY new to RxJs so I'm sure I'm just doing something completely stupid there.



I'm getting theerror:




TS2322: Type 'Observable<EhsAssessmentAr>' is not assignable to type 'Observable<EhsAssessmentAr>`











share|improve this question





















  • Possible duplicate of Observable<> not assignable to type Observable<SomeType>
    – paulsm4
    Nov 10 at 1:17










  • Doesn’t seem the same to me. I declared as an array return type and I feel like I am returning an array type. What you pointed at was returning array or object.
    – Gargoyle
    Nov 10 at 1:24










  • did you check stackoverflow.com/a/43233448/4591364 ?
    – Microsmsm
    Nov 10 at 1:37










  • were you be able to fix the problem?
    – Microsmsm
    Nov 10 at 2:27










  • Not at my computer but I think the issue is the map I used takes the array not a single object like the “normal” map
    – Gargoyle
    Nov 10 at 2:28












up vote
0
down vote

favorite









up vote
0
down vote

favorite











When I make a GET call via HttpClient I want to pass back an array of the actual typed objects, vs. just a generic 'object', and so I tried to do this:



getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
return this.http
.get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
.pipe(
map(x =>
const ret = EhsAssessmentAr.fromJson(x);
ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
return ret;
)
)



That fromJson method is declared to return the proper class, and I thought since I was sending this through map that I'd get back an array. I'm REALLY new to RxJs so I'm sure I'm just doing something completely stupid there.



I'm getting theerror:




TS2322: Type 'Observable<EhsAssessmentAr>' is not assignable to type 'Observable<EhsAssessmentAr>`











share|improve this question













When I make a GET call via HttpClient I want to pass back an array of the actual typed objects, vs. just a generic 'object', and so I tried to do this:



getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
return this.http
.get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
.pipe(
map(x =>
const ret = EhsAssessmentAr.fromJson(x);
ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
return ret;
)
)



That fromJson method is declared to return the proper class, and I thought since I was sending this through map that I'd get back an array. I'm REALLY new to RxJs so I'm sure I'm just doing something completely stupid there.



I'm getting theerror:




TS2322: Type 'Observable<EhsAssessmentAr>' is not assignable to type 'Observable<EhsAssessmentAr>`








angular rxjs rxjs6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 1:14









Gargoyle

2,65942647




2,65942647











  • Possible duplicate of Observable<> not assignable to type Observable<SomeType>
    – paulsm4
    Nov 10 at 1:17










  • Doesn’t seem the same to me. I declared as an array return type and I feel like I am returning an array type. What you pointed at was returning array or object.
    – Gargoyle
    Nov 10 at 1:24










  • did you check stackoverflow.com/a/43233448/4591364 ?
    – Microsmsm
    Nov 10 at 1:37










  • were you be able to fix the problem?
    – Microsmsm
    Nov 10 at 2:27










  • Not at my computer but I think the issue is the map I used takes the array not a single object like the “normal” map
    – Gargoyle
    Nov 10 at 2:28
















  • Possible duplicate of Observable<> not assignable to type Observable<SomeType>
    – paulsm4
    Nov 10 at 1:17










  • Doesn’t seem the same to me. I declared as an array return type and I feel like I am returning an array type. What you pointed at was returning array or object.
    – Gargoyle
    Nov 10 at 1:24










  • did you check stackoverflow.com/a/43233448/4591364 ?
    – Microsmsm
    Nov 10 at 1:37










  • were you be able to fix the problem?
    – Microsmsm
    Nov 10 at 2:27










  • Not at my computer but I think the issue is the map I used takes the array not a single object like the “normal” map
    – Gargoyle
    Nov 10 at 2:28















Possible duplicate of Observable<> not assignable to type Observable<SomeType>
– paulsm4
Nov 10 at 1:17




Possible duplicate of Observable<> not assignable to type Observable<SomeType>
– paulsm4
Nov 10 at 1:17












Doesn’t seem the same to me. I declared as an array return type and I feel like I am returning an array type. What you pointed at was returning array or object.
– Gargoyle
Nov 10 at 1:24




Doesn’t seem the same to me. I declared as an array return type and I feel like I am returning an array type. What you pointed at was returning array or object.
– Gargoyle
Nov 10 at 1:24












did you check stackoverflow.com/a/43233448/4591364 ?
– Microsmsm
Nov 10 at 1:37




did you check stackoverflow.com/a/43233448/4591364 ?
– Microsmsm
Nov 10 at 1:37












were you be able to fix the problem?
– Microsmsm
Nov 10 at 2:27




were you be able to fix the problem?
– Microsmsm
Nov 10 at 2:27












Not at my computer but I think the issue is the map I used takes the array not a single object like the “normal” map
– Gargoyle
Nov 10 at 2:28




Not at my computer but I think the issue is the map I used takes the array not a single object like the “normal” map
– Gargoyle
Nov 10 at 2:28












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You can strong type your mapping response and Check you are returning array not single element:






getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
return this.http
.get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
.pipe(
map((x:EhsAssessmentAr) =>
const ret = EhsAssessmentAr.fromJson(x);
ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
return ret;
)
)





Pro-tip: You can also use map directly after importing from rxjs






import "rxjs/add/operator/map";
this.http.get(url).map((json: TypedArray) =>
//json parsed
return jsonParsed;
);





Pro-tip 2: If you are changing return type for any reason you can use:



return <TypedArray> arrParsed["something"];


Pro-tip 3: Angular hates you feed it with html so you need to find some cool solution like when templating adding a replace of token to new line






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235191%2fhow-do-i-pipe-map-to-return-an-array-in-angular-7%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You can strong type your mapping response and Check you are returning array not single element:






    getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
    return this.http
    .get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
    .pipe(
    map((x:EhsAssessmentAr) =>
    const ret = EhsAssessmentAr.fromJson(x);
    ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
    return ret;
    )
    )





    Pro-tip: You can also use map directly after importing from rxjs






    import "rxjs/add/operator/map";
    this.http.get(url).map((json: TypedArray) =>
    //json parsed
    return jsonParsed;
    );





    Pro-tip 2: If you are changing return type for any reason you can use:



    return <TypedArray> arrParsed["something"];


    Pro-tip 3: Angular hates you feed it with html so you need to find some cool solution like when templating adding a replace of token to new line






    share|improve this answer


























      up vote
      0
      down vote



      accepted










      You can strong type your mapping response and Check you are returning array not single element:






      getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
      return this.http
      .get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
      .pipe(
      map((x:EhsAssessmentAr) =>
      const ret = EhsAssessmentAr.fromJson(x);
      ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
      return ret;
      )
      )





      Pro-tip: You can also use map directly after importing from rxjs






      import "rxjs/add/operator/map";
      this.http.get(url).map((json: TypedArray) =>
      //json parsed
      return jsonParsed;
      );





      Pro-tip 2: If you are changing return type for any reason you can use:



      return <TypedArray> arrParsed["something"];


      Pro-tip 3: Angular hates you feed it with html so you need to find some cool solution like when templating adding a replace of token to new line






      share|improve this answer
























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        You can strong type your mapping response and Check you are returning array not single element:






        getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
        return this.http
        .get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
        .pipe(
        map((x:EhsAssessmentAr) =>
        const ret = EhsAssessmentAr.fromJson(x);
        ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
        return ret;
        )
        )





        Pro-tip: You can also use map directly after importing from rxjs






        import "rxjs/add/operator/map";
        this.http.get(url).map((json: TypedArray) =>
        //json parsed
        return jsonParsed;
        );





        Pro-tip 2: If you are changing return type for any reason you can use:



        return <TypedArray> arrParsed["something"];


        Pro-tip 3: Angular hates you feed it with html so you need to find some cool solution like when templating adding a replace of token to new line






        share|improve this answer














        You can strong type your mapping response and Check you are returning array not single element:






        getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
        return this.http
        .get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
        .pipe(
        map((x:EhsAssessmentAr) =>
        const ret = EhsAssessmentAr.fromJson(x);
        ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
        return ret;
        )
        )





        Pro-tip: You can also use map directly after importing from rxjs






        import "rxjs/add/operator/map";
        this.http.get(url).map((json: TypedArray) =>
        //json parsed
        return jsonParsed;
        );





        Pro-tip 2: If you are changing return type for any reason you can use:



        return <TypedArray> arrParsed["something"];


        Pro-tip 3: Angular hates you feed it with html so you need to find some cool solution like when templating adding a replace of token to new line






        getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
        return this.http
        .get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
        .pipe(
        map((x:EhsAssessmentAr) =>
        const ret = EhsAssessmentAr.fromJson(x);
        ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
        return ret;
        )
        )





        getExistingArsByLab(labId: number): Observable<EhsAssessmentAr> 
        return this.http
        .get<EhsAssessmentAr>(`$this.baseUrl/ar?labId=$labId`)
        .pipe(
        map((x:EhsAssessmentAr) =>
        const ret = EhsAssessmentAr.fromJson(x);
        ret.ar = this.sanitizer.sanitize(SecurityContext.HTML, ret.ar.replace(/n/g, '<br/>'));
        return ret;
        )
        )





        import "rxjs/add/operator/map";
        this.http.get(url).map((json: TypedArray) =>
        //json parsed
        return jsonParsed;
        );





        import "rxjs/add/operator/map";
        this.http.get(url).map((json: TypedArray) =>
        //json parsed
        return jsonParsed;
        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 1:34

























        answered Nov 10 at 1:21









        Microsmsm

        1,8871624




        1,8871624



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235191%2fhow-do-i-pipe-map-to-return-an-array-in-angular-7%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Peter Parker: The Spectacular Spider-Man #308