Can someone help me with a quick solution to this Angular 4 issue?
title:string;
content: string;
comment: string;
constructor( private http: HttpClient) {}
posts: {title: string, content: string, comments: String[]}[] = [];
ngOnInit() {
this.http.get('http://localhost:3003/posts').subscribe( res => {
console.log(res))
// can't do res.forEach (object has no forEach)
// can't do JSON.parse(res) because res is not a string
})
}
The response content:
["{"title":"sadsa","content":"sadasd","comments":[]}", "{"title":"sadsa","content":"sadasd","comments":[]}", "{"title":"sadsa","content":"sadasd","comments":[]}", "{"title":"sadsa","content":"sadasd","comments":[]}", "{"title":"sadsa","content":"sadasd","comments":[]}", "{"title":"sadsa","content":"sadasd","comments":[]}", "{"title":"sadsa","content":"sadasd","comments":[]}"]
I am trying to handle an array received from the server that matches the client-side array, but it is coming in a stringified format. TypeScript is not recognizing it as an array and claims it to be an object. I also attempted sending an object containing the array, but encountered an 'unresolved variable' error. What could be missing in my approach?