TypeError: Cannot read property 'uid' of null
at SwitchMapSubscriber.project (auth.service.ts:37)
at SwitchMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/switchMap.js.SwitchMapSubscriber._next
(switchMap.js:34)
at SwitchMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next
(Subscriber.js:54)
Вот код
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase';
import { Observable } from 'rxjs/Observable';
import { ActivatedRoute } from '@angular/router';
import { UserService } from './user.service';
import { AppUser } from './models/app-user';
import 'rxjs/add/operator/switchMap';
@Injectable()
export class AuthService {
user$: Observable<firebase.User>;
constructor(
private userService:UserService,
private afAuth: AngularFireAuth,
private route:ActivatedRoute
) {
this.user$ = afAuth.authState;
}
login(){
let returnUrl = this.route.snapshot.queryParamMap.get('returnUrl') || '/';
localStorage.setItem('returnUrl',returnUrl);
this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider());
}
logout(){
this.afAuth.auth.signOut();
}
get appUser$() : Observable< AppUser >{
return this.user$
.switchMap(user => this.userService.get(user.uid).valueChanges());
}
}
UserService
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import * as firebase from 'firebase';
import { AppUser} from './models/app-user';
@Injectable()
export class UserService {
constructor(private db:AngularFireDatabase) { }
save(user: firebase.User){
this.db.object('/users/' +user.uid).update({
name:user.displayName,
email:user.email
});
}
get(uid: string): AngularFireObject<AppUser>{
return this.db.object('/users/'+ uid);
}
}