Reputation: 49
I have an error about property who doesn't exist on type Post[]. All code works perfectly, but I have this errors in my console.
Service.ts
private objectAllPosts: BehaviorSubject<Post[]>;
public allPosts: Observable<Post[]>;
private listPosts: Post[] = [];
public tempPosts: Post[] = [];
constructor(private _http: HttpClient, private _toast: ToastService) {
this.objectAllPosts = new BehaviorSubject(null) as BehaviorSubject<Post[]>;
this.allPosts = this.objectAllPosts.asObservable();
}
public getAllPosts() {
this._http.get<Post[]>(environment.apiUrl + "/api/posts/").subscribe(
(res) => {
this.listPosts = res.posts; // MESSAGE ERROR
this.tempPosts = [...res.posts]; // MESSAGE ERROR
this.objectAllPosts.next(this.listPosts); // MESSAGE ERROR
},
(error) => {
console.log(error);
}
);
}
Interface
export class Post {
public _id?: string;
public post: string;
public posts: [];
public comments?: object;
public createdAt?: object;
}
Upvotes: 0
Views: 1082
Reputation: 31115
It is a TS Lint error. The correct way to solve it would be define the corresponding types. Another way to avoid it would be to use bracket notation instead of dot notation. Find more details on property accessors here.
Try the following
this.listPosts = res['posts'];
this.tempPosts = [...res['posts']];
Upvotes: 1
Reputation: 222592
Simply you can fix this error If you don't care about typing, by adding :any
(res : any) => {
But, the best practice is to have those types defined in your domain models. You can replace :any with the type you defined.
Upvotes: 0