First, this is an Angular2 app. I am just starting out building with Angular2 and Firebase. I am using AngularFire2 as well. I know that my service is working, because I can print out the blog.title
just fine right after subscribing. But that is the only place I can get access to the results. Everywhere else it is giving me an undefined
error. Whether I use it in the template with {{}} or in the class, I get the same same result. I am not sure what I am doing wrong. I am also new to observables.
export class BlogEditComponent implements OnInit {
blogForm: FormGroup;
blog$: FirebaseObjectObservable<Blog>;
blog: Blog;
constructor(private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private blogService: BlogService) { }
ngOnInit() {
this.isNew = false;
this.blog$ = this.blogService.getBlogFromId(this.route.snapshot.params['id']);
this.blog$.subscribe(snapshot =>{
this.blog = snapshot;
console.log(this.blog.title); //prints out fine
});
console.log(this.blog.title); //throws error here
}
}
BlogService
getBlogFromId(id: String): FirebaseObjectObservable<Blog> {
return this.af.database.object('blogs/' + id);
}