我正在制作一个 Web 应用程序,其中有一个 GitHub 的授权按钮,可以访问个人 GitHub 存储库。这些存储库将显示在网络应用程序上。
import {Component, OnInit} from '@angular/core';
import {DataService} from '../../services/data/data.service';
import {AuthService} from '../../services/auth/auth.service';
import {DatabaseService} from '../../services/share/database.service';
import {AuthdataDto} from '../../services/dto/authdata.dto';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-repositories',
templateUrl: './repositories.component.html',
styleUrls: ['./repositories.component.sass']
})
export class RepositoriesComponent implements OnInit {
repositories: Object;
authData: AuthdataDto;
//loggedin: boolean;
constructor(private data: DataService, public authService:
AuthService, private dbService: DatabaseService) { }
ngOnInit() {
this.getSomething().subscribe(data => {
this.data.getrepositories(this.authService.token,
this.authService.username)});
}
getSomething(){
return this.dbService.getFromDatabase('user',
this.authService.userDetails.uid).pipe(
map((data => {this.authData = data})));
}
}
this is the error i get 我试图首先通过组件中的 doSomething() 方法检索所有数据,然后 ngOnInit() 方法应该获取存储库。但是那是行不通的。
有什么建议吗?
编辑:这是 AuthService 类。
import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import {AngularFireAuth} from 'angularfire2/auth';
import {Router} from '@angular/router';
import {Observable, throwError} from 'rxjs';
import {UserDto} from '../dto/user.dto';
import {DatabaseService} from '../share/database.service';
import {AuthdataDto} from '../dto/authdata.dto';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private _user: Observable<firebase.User>;
private _userdata: UserDto;
private _userDetails: firebase.User = null;
private _username;
private _token;
constructor(private _afAuth: AngularFireAuth, private router:
Router, private databaseService: DatabaseService) {
this._user = _afAuth.authState;
this._user.subscribe(
(user) => {
if (user) {
this._userDetails = user;
} else {
this._userDetails = null;
}
}
);
}
public loginwithGithub() {
return new Promise<any>((resolve, reject) => {
this.loginwithGithubProvider().then(
res => {
this.userdata = new
UserDto().deserialize(JSON.parse(JSON.stringify(this.userDetails)));
this.databaseService.pushToDatabase('user', 'tim',
this.userdata);
console.log(this.databaseService.getFromDatabase('user',
'kevin'));
resolve(res);
}, err => {
console.log(err);
reject(err);
});
});
}
public loginwithGithubProvider():
Promise<firebase.auth.UserCredential> {
return new Promise<any>((resolve, reject) => {
this._afAuth.auth.signInWithPopup(
new firebase.auth.GithubAuthProvider()).then(res => {
const data = new AuthdataDto(res.additionalUserInfo.username,
res.credential['accessToken'])
this.databaseService.pushToDatabase('user', res.user.uid,
data);
}, err => {
console.log(err);
reject(err);
});
});
}
public logout(): Promise<boolean | Observable<never> | never> {
return this._afAuth.auth.signOut()
.then((res) => this.router.navigate(['/'])
.catch((err) => throwError('signout failed')));
}
public get isLoggedIn(): boolean {
return this._userDetails != null;
}
get user(): Observable<firebase.User> {
return this._user;
}
get userDetails(): firebase.User {
return this._userDetails;
}
get userdata(): UserDto {
return this._userdata;
}
set userdata(value: UserDto) {
this._userdata = value;
}
get token() {
return this._token;
}
set token(value) {
this._token = value;
}
get username() {
return this._username;
}
set username(value) {
this._username = value;
}
get afAuth(): AngularFireAuth {
return this._afAuth;
}
set afAuth(value: AngularFireAuth) {
this._afAuth = value;
}
}
最佳答案
检查您的 .subscribe()。如果仔细观察,您可能会注意到 “数据” 呈灰色显示,这意味着它从未被使用过。
关于Angular 检索数据问题(Cannot read property 'uid' of null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53516096/