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());
}
}
用户服务
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);
}
}
答案 0 :(得分:0)
只需添加一个if检查用户是否可用,然后您使用ID进行请求,
get appUser$(): Observable<AppUser>{
return this.user$
.switchMap(user => {
if(user) return this.UserService.get(user.uid)
return Observable.of(null);
})
}
答案 1 :(得分:0)
get appUser$(): Observable<AppUser>{
return this.user$
.switchMap(user => {
if(user) return this.userService.get(user.uid);
return Observable.of(null);
})
}
答案 2 :(得分:0)
有很多更新,所以在2021年,实现相同功能的方式如下
struct CrossingIter<'a, T> {
index: usize,
iter: std::iter::Skip<std::slice::Iter<'a, T>>,
}