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>`
angular rxjs rxjs6
add a comment |
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>`
angular rxjs rxjs6
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
add a comment |
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>`
angular rxjs rxjs6
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
angular rxjs rxjs6
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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;
);edited Nov 10 at 1:34
answered Nov 10 at 1:21
Microsmsm
1,8871624
1,8871624
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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