subscribe not working on FirebaseObjectObservable
I have a form by which an user can update their existing data.
<input type="text" class="form-control" id="name" [(ngModel)]="memberPristine.name"
#name="ngForm" ngControl="name">
Component class for this form
form: ControlGroup;
memberPristine: {
name: ''
};
constructor(private fb: FormBuilder, private userService: UserService) {
}
ngOnInit() {
this.userService.member$.subscribe(x=>{
this.memberPristine = x;
});
}
user service class
export class UserService {
member$: FirebaseObjectObservable<any>;
constructor(public af: AngularFire) {
this.af.auth.subscribe(a => {
this.member$ = this.af.database.object('/members/'+a.uid);
});
}
When I run this I got an error.
ORIGINAL EXCEPTION: TypeError: Cannot read property 'subscribe' of undefined.
@ohanspace I'm guessing it's because member$ is undefined until it's set inside of the subscriber in your UserService constructor.
In this example, if you aren't writing data to the /members/ collection, you could rewrite your service like so:
import { Observable } from 'rxjs/Observable';
export class UserService {
member$: Observable<any>;
constructor(af: AngularFire) {
this.member$ = af.auth.map(a => af.database.object('/members/'+a.uid));
}
}
Then your component could look something like:
class MyComponent {
form: ControlGroup;
memberPristine: {
name: ''
};
memberSub: Subscription<any>;
constructor(private fb: FormBuilder, private userService: UserService) {
}
ngOnInit() {
this.memberSub = this.userService.member$.subscribe(x=>{
this.memberPristine = x;
});
}
ngOnDestroy() {
this.memberSub.dispose();
}
}
The reason this pattern doesn't always work is that the type of observable you'll get back won't be a FirebaseObjectObservable, and won't contain the setter methods (update, set, remove), since the source observable from af.auth is just a ReplaySubject from rxjs.
thank you so much boss @jeffbcross :)