On a local MEAN stack app using NodeJS server, my endpoint response returns array os Post:
{
"message": "Posts fetched successfully!",
"posts": [
{
"_id": "618b22ff9e50776ed377c4eb",
"title": "aaaaaaaaaaaaaa",
"body": "xxxxxxxxxxxxx",
"__v": 0
},
]
}
Angular Service with a response Model:
export interface PostResponse {
message: string;
posts: Post;
}
getPosts(): Observable<PostResponse> {
return this.http.get<PostResponse>(this.postsUrl + '/api/posts');
}
PostComponent inject the above Postservice:
export interface Post {
id: number;
title: string;
body: string;
}
export class PostsComponent implements OnInit {
posts: Post[] = [];
constructor(private postService: PostService) {}
ngOnInit() {
this.postService
.getPosts()
.pipe(
map((data) => {
console.log('data : ', data.posts._id); // vide #1
return {
id: data.posts.id, // _id error <HERE>
title: data.posts.title,
content: data.posts.body,
};
})
).subscribe((response) => {
this.posts = response; // error <HERE>
console.log('posts updated : ', this.posts);
});
}
Question.
Q-1 : On my Component when I map response data, the prop required is data.posts.id, instead of data.posts._id, but the stream data is: #1 { "_id": "618b22ff9e50776ed377c4eb", "title": "aaaaaaaaaaaaaa", "body": "xxxxxxxxxxxxx", "__v": 0 },
if I use _id, I get a error Property '_id' does not exist on type 'Post'
is this because of my interface PostResponse?
_id is comming from my mongo database, I was assuming that I had to force transforming response data to match the front-end structure where I don't use _ID but ID.
Q-2: on the subscribe, on this.posts = response I'm getting error:
Type '{ id: number; title: string; content: string; }' is missing the following properties from type 'Post[]': length, pop, push, concat, and 26 more.ts(2740)