Can someone help me understand why it's saying that "post" is not defined on line 20 in my home.component.ts file? Here is the code below. Any input would be appreciated, thank you!
home.component.ts
import { Component, OnInit } from '@angular/core';
import { Post } from '../_models/index';
import { PostService } from '../_services/index';
@Component({
moduleId: module.id,
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit {
posts: Post[] = [];
constructor(private postService: PostService) { }
ngOnInit() {
// get posts from secure api end point
this.postService.getPosts().subscribe(users => {
this.posts = post;
});
}
}
post.ts
import { Injectable } from '@angular/core';
import {Author} from "./author";
@Injectable()
export class Post {
id:number;
author:Author;
subscribed:boolean;
created:string;
active:boolean;
text:string;
comments:string[];
constructor(id:number, author:Author, subscribed:boolean, created:string, active:boolean, text:string, comments:string[]) {
this.id = id;
this.author = author;
this.subscribed = subscribed;
this.created = created;
this.active = active;
this.text = text;
this.comments = comments;
}
public static getPosts():Post{
return new Post(0, null, null, "", null, "",null);
}
}
post.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import { AuthenticationService } from './index';
import { Post } from '../_models/index';
@Injectable()
export class PostService {
constructor( private http: Http, private authenticationService: AuthenticationService) {}
getPosts(): Observable<Post[]> {
// add authorization header with jwt token
let headers = new Headers();
headers.append('Authorization','token ' + this.authenticationService.token);
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({
headers: headers
}); // console.log(headers);
// get posts from api
return this.http.get('http://localhost:8000/areas/fun/', options)
.map((response: Response) => response.json());
}
}
If you need more information, feel free to ask. Thanks!