Type 'Post[]' is not assignable to type 'Observable': error TS2322
up vote
0
down vote
favorite
I have googled both the message and error and it seems the issue I keep finding is not what I am dealing with.
I am an angular noob and feel I am finally close to getting my service wired up.
I am getting the above error on my component
it will squawk at the this.posts
below.
getPosts() {
this.postsService.get()
.subscribe((data: Post) => this.posts = data);};
and my service in case theres an issue there.
public get(): Observable<Post> {
return this.http
.get<Post>(this.accessPointUrl);}
Someone save my morning please!
angular typescript
|
show 12 more comments
up vote
0
down vote
favorite
I have googled both the message and error and it seems the issue I keep finding is not what I am dealing with.
I am an angular noob and feel I am finally close to getting my service wired up.
I am getting the above error on my component
it will squawk at the this.posts
below.
getPosts() {
this.postsService.get()
.subscribe((data: Post) => this.posts = data);};
and my service in case theres an issue there.
public get(): Observable<Post> {
return this.http
.get<Post>(this.accessPointUrl);}
Someone save my morning please!
angular typescript
1
What type isthis.posts
? I'm guessingObservable<Post>
?
– user184994
Nov 8 at 11:52
1
@JeffLongo Change the type ofthis.posts
fromposts: Observable<Post>
toposts: Post
– user184994
Nov 8 at 12:02
1
Add pipe operator in your service - that will helpthis.http.get<Post>(this.accessPointUrl).pipe();
– Rahul Swamynathan
Nov 8 at 12:08
1
In that case, change the type back toObservable<Post>
, and remove thesubscribe
, so it becomesthis.posts = this.postsService.get()
– user184994
Nov 8 at 12:09
1
When you say it's logging undefined, where are you logging? Is it inside the subscribe function, or outside? Because the code is asynchronous, so if you try to log it outside of the subscribe function it will never work
– user184994
Nov 8 at 12:21
|
show 12 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have googled both the message and error and it seems the issue I keep finding is not what I am dealing with.
I am an angular noob and feel I am finally close to getting my service wired up.
I am getting the above error on my component
it will squawk at the this.posts
below.
getPosts() {
this.postsService.get()
.subscribe((data: Post) => this.posts = data);};
and my service in case theres an issue there.
public get(): Observable<Post> {
return this.http
.get<Post>(this.accessPointUrl);}
Someone save my morning please!
angular typescript
I have googled both the message and error and it seems the issue I keep finding is not what I am dealing with.
I am an angular noob and feel I am finally close to getting my service wired up.
I am getting the above error on my component
it will squawk at the this.posts
below.
getPosts() {
this.postsService.get()
.subscribe((data: Post) => this.posts = data);};
and my service in case theres an issue there.
public get(): Observable<Post> {
return this.http
.get<Post>(this.accessPointUrl);}
Someone save my morning please!
angular typescript
angular typescript
asked Nov 8 at 11:50
Jeff Longo
6210
6210
1
What type isthis.posts
? I'm guessingObservable<Post>
?
– user184994
Nov 8 at 11:52
1
@JeffLongo Change the type ofthis.posts
fromposts: Observable<Post>
toposts: Post
– user184994
Nov 8 at 12:02
1
Add pipe operator in your service - that will helpthis.http.get<Post>(this.accessPointUrl).pipe();
– Rahul Swamynathan
Nov 8 at 12:08
1
In that case, change the type back toObservable<Post>
, and remove thesubscribe
, so it becomesthis.posts = this.postsService.get()
– user184994
Nov 8 at 12:09
1
When you say it's logging undefined, where are you logging? Is it inside the subscribe function, or outside? Because the code is asynchronous, so if you try to log it outside of the subscribe function it will never work
– user184994
Nov 8 at 12:21
|
show 12 more comments
1
What type isthis.posts
? I'm guessingObservable<Post>
?
– user184994
Nov 8 at 11:52
1
@JeffLongo Change the type ofthis.posts
fromposts: Observable<Post>
toposts: Post
– user184994
Nov 8 at 12:02
1
Add pipe operator in your service - that will helpthis.http.get<Post>(this.accessPointUrl).pipe();
– Rahul Swamynathan
Nov 8 at 12:08
1
In that case, change the type back toObservable<Post>
, and remove thesubscribe
, so it becomesthis.posts = this.postsService.get()
– user184994
Nov 8 at 12:09
1
When you say it's logging undefined, where are you logging? Is it inside the subscribe function, or outside? Because the code is asynchronous, so if you try to log it outside of the subscribe function it will never work
– user184994
Nov 8 at 12:21
1
1
What type is
this.posts
? I'm guessing Observable<Post>
?– user184994
Nov 8 at 11:52
What type is
this.posts
? I'm guessing Observable<Post>
?– user184994
Nov 8 at 11:52
1
1
@JeffLongo Change the type of
this.posts
from posts: Observable<Post>
to posts: Post
– user184994
Nov 8 at 12:02
@JeffLongo Change the type of
this.posts
from posts: Observable<Post>
to posts: Post
– user184994
Nov 8 at 12:02
1
1
Add pipe operator in your service - that will help
this.http.get<Post>(this.accessPointUrl).pipe();
– Rahul Swamynathan
Nov 8 at 12:08
Add pipe operator in your service - that will help
this.http.get<Post>(this.accessPointUrl).pipe();
– Rahul Swamynathan
Nov 8 at 12:08
1
1
In that case, change the type back to
Observable<Post>
, and remove the subscribe
, so it becomes this.posts = this.postsService.get()
– user184994
Nov 8 at 12:09
In that case, change the type back to
Observable<Post>
, and remove the subscribe
, so it becomes this.posts = this.postsService.get()
– user184994
Nov 8 at 12:09
1
1
When you say it's logging undefined, where are you logging? Is it inside the subscribe function, or outside? Because the code is asynchronous, so if you try to log it outside of the subscribe function it will never work
– user184994
Nov 8 at 12:21
When you say it's logging undefined, where are you logging? Is it inside the subscribe function, or outside? Because the code is asynchronous, so if you try to log it outside of the subscribe function it will never work
– user184994
Nov 8 at 12:21
|
show 12 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
just return the http.
return this.http
.get(this.accessPointUrl);}
Declare the type of posts
as post
posts: Post;
add a comment |
up vote
0
down vote
There are two options you either change your posts
variable to Post
or you dont subscribe to the get()
method of your service and directly assign it to your observable variable like this:
this.posts = this.postsService.get();
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
just return the http.
return this.http
.get(this.accessPointUrl);}
Declare the type of posts
as post
posts: Post;
add a comment |
up vote
0
down vote
just return the http.
return this.http
.get(this.accessPointUrl);}
Declare the type of posts
as post
posts: Post;
add a comment |
up vote
0
down vote
up vote
0
down vote
just return the http.
return this.http
.get(this.accessPointUrl);}
Declare the type of posts
as post
posts: Post;
just return the http.
return this.http
.get(this.accessPointUrl);}
Declare the type of posts
as post
posts: Post;
answered Nov 8 at 11:51
Sachila Ranawaka
17.6k32043
17.6k32043
add a comment |
add a comment |
up vote
0
down vote
There are two options you either change your posts
variable to Post
or you dont subscribe to the get()
method of your service and directly assign it to your observable variable like this:
this.posts = this.postsService.get();
add a comment |
up vote
0
down vote
There are two options you either change your posts
variable to Post
or you dont subscribe to the get()
method of your service and directly assign it to your observable variable like this:
this.posts = this.postsService.get();
add a comment |
up vote
0
down vote
up vote
0
down vote
There are two options you either change your posts
variable to Post
or you dont subscribe to the get()
method of your service and directly assign it to your observable variable like this:
this.posts = this.postsService.get();
There are two options you either change your posts
variable to Post
or you dont subscribe to the get()
method of your service and directly assign it to your observable variable like this:
this.posts = this.postsService.get();
answered Nov 8 at 12:00
locohost
235
235
add a comment |
add a comment |
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%2f53207168%2ftype-post-is-not-assignable-to-type-observablepost-error-ts2322%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
1
What type is
this.posts
? I'm guessingObservable<Post>
?– user184994
Nov 8 at 11:52
1
@JeffLongo Change the type of
this.posts
fromposts: Observable<Post>
toposts: Post
– user184994
Nov 8 at 12:02
1
Add pipe operator in your service - that will help
this.http.get<Post>(this.accessPointUrl).pipe();
– Rahul Swamynathan
Nov 8 at 12:08
1
In that case, change the type back to
Observable<Post>
, and remove thesubscribe
, so it becomesthis.posts = this.postsService.get()
– user184994
Nov 8 at 12:09
1
When you say it's logging undefined, where are you logging? Is it inside the subscribe function, or outside? Because the code is asynchronous, so if you try to log it outside of the subscribe function it will never work
– user184994
Nov 8 at 12:21