从火力检索用户简档数据和在火力与此代码显示
问题描述:
林创建用户配置文件:从火力检索用户简档数据和在火力与此代码显示
username: string
msgnumber: number
level: number
constructor(private fire:AngularFireAuth,private db :AngularFireDatabase,public navCtrl: NavController, public navParams: NavParams) {
}
createProfile() {
this.fire.authState.take(1).subscribe(auth => {
this.db.object(`profile/${auth.uid}`).set({
username: this.username,
msgnumber: 0,
level: 0
}).then(() =>
this.navCtrl.setRoot(TabsPage));
})
}
它正在工作。现在我试图将用户的数据显示在他们的个人资料页面上。
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase,FirebaseListObservable} from 'angularfire2/database';
@Component({
selector: 'page-profile',
templateUrl: 'profile.html'
})
export class ProfilePage {
profileData: FirebaseListObservable<any>
constructor(private fire:AngularFireAuth,private db :AngularFireDatabase,public navCtrl: NavController) {
this.fire.authState.subscribe(auth =>{
this.profileData = this.db.list(`profile/${auth.uid}`);
console.log(this.profileData);
});
}
}
我试过类似的东西,但看起来不工作。问题是我想我无法得到对象/列表可观察对象的概念,或者一般如何检索数据以及对象可观察对象如何工作。检查互联网,我猜人们正在使用它们来检索数据?
数据库: db
答
你是在正确的道路。为了最终检索您需要的数据到subscribe
到分配给this.profileData
的第二个可观察值。
像这样:
this.fire.authState.subscribe(auth => {
// subscribe to the observable
this.db.list(`profile/${auth.uid}`).valueChanges().subscribe(profile => {
console.log(profile);
});
});
我们避免你可以使用switchMap
算子rxjs为您提供的嵌套。
结果看起来是这样的:
this.profileData$ = this.fire.authState.switchMap(auth => this.db.list(`profile/${auth.uid}`).valueChanges());
this.profileData$.subscribe(profile => console.log(profile))
感谢answer.i尝试这样做,得到了_this.db.list(...)认购不是一个函数错误。你有什么主意吗 ? –
您正在使用哪个版本的'angularfire2'? – Orlandster
最新one.5我猜 –