I encountered an error while creating a new Angular project following a tutorial, and I'm seeking assistance to understand it.
The error message reads: "No overload matches this call. Overload 1 of 5... Type 'Object' is missing the following properties from type 'PostModel[]': length, pop, push, concat, and more."
Post Model:
import {UserModel} from './user.model';
export class PostModel {
constructor(
public id: number,
public title: string,
public content: string,
public image?: string,
public user?: UserModel,
public created_at?: string,
public updated_at?: string,
) {}
}
Static Service:
import { Injectable } from '@angular/core';
import {HttpHeaders} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class StaticService {
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
// 'Authorization': 'Basic ' + btoa('test:123456')
})
};
baseUrl = 'http://localhost:8080/';
constructor() { }
}
Post Service:
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Subject} from 'rxjs';
import {PostModel} from '../models/post.model';
import {StaticService} from './static.service';
@Injectable({
providedIn: 'root'
})
export class PostService {
private posts = new Subject<PostModel[]>();
public posts$ = this.posts.asObservable();
constructor(private http: HttpClient, private staticService: StaticService) {}
getPosts() {
this.http.get(this.staticService.baseUrl + 'posts/all', this.staticService.httpOptions).subscribe(
(response: PostModel[]) => {
this.posts.next(response);
}, (error) => {
console.log(error);
}
);
return this.posts$;
}
getPost(id: number) {
return this.http.get(this.staticService.baseUrl + 'posts/' + id, this.staticService.httpOptions);
}
savePost(post: PostModel) {
return this.http.post(this.staticService.baseUrl + 'posts', post, this.staticService.httpOptions);
}
updatePost(post: PostModel) {
return this.http.put(this.staticService.baseUrl + 'posts/' + post.id, post, this.staticService.httpOptions);
}
deletePost(id: number) {
return this.http.delete(this.staticService.baseUrl + 'posts/' + id, this.staticService.httpOptions);
}
}