【发布时间】:2019-02-17 17:44:52
【问题描述】:
所以,我有以下代码可以从我的 Firebase 用户列表中检索数据:
currentUserRef: AngularFireList<any>
currentUser: Observable<any>;
var user = firebase.auth().currentUser;
this.currentUserRef = this.af.list('usuarios', ref =>
ref.orderByChild('email').equalTo(user.email));
this.currentUser = this.currentUserRef.valueChanges();
this.currentUser.subscribe(res => console.log(res[0].condominio));
在检索到的数据中,我有两个属性:condominio 和 email。
在console.log(res[0].condominio)我有数据输出到控制台,但是我需要把它保存到一个变量中,例如:
userCond: string;
然后我必须从 res 中获取属性 'condominio' 并将其设置为 'userCond',但如果我尝试这样做它不起作用:
this.currentUser.subscribe((res) => { this.userCond = res[0].condominio });
我做错了什么?
"angularfire2": "^5.0.0-rc.11",
"firebase": "^5.3.1",
"ionic-angular": "3.9.2",
编辑:
export class HomePage {
currentUserRef: AngularFireList<any>
currentUser: Observable<any>;
moradorRef: AngularFireList<any>
morador: Observable<any>;
userCond: string;
constructor(
public authService: AuthService,
public userService: UserService,
public navCtrl: NavController,
public navParams: NavParams,
public af: AngularFireDatabase
) {
}
ionViewCanEnter(): Promise<boolean> {
return this.authService.authenticated;
}
ionViewDidLoad(){
debugger;
//TRAZ AS MESMAS INFORMAÇÕES
//this.userService.getCurrentUser();
var user = firebase.auth().currentUser;
//const _this = this;
this.currentUserRef = this.af.list('usuarios', ref =>
ref.orderByChild('email').equalTo(user.email));
this.currentUser = this.currentUserRef.valueChanges();
//this.currentUser.subscribe(res =>
console.log(res[0].condominio));
this.currentUser.subscribe(res => {
debugger;
this.userCond = res[0].condominio;
});
console.log(this.userCond);
【问题讨论】:
-
我不知道 Firebase,但什么不起作用?您是否尝试在 HTML 中检索
userCond的值?console.log(this.userCond)没有在订阅下打印所需的输出。您是否尝试过在 HTML 中进行安全导航? angular.io/guide/template-syntax#safe-navigation-operator -
不,首先我从当前登录的用户那里得到电子邮件,然后我必须去我的用户的firebase列表中搜索该电子邮件,一旦找到从那里获取'condominio'属性那个项目。我不想在我的 HTML 中使用它,我只需要从 firebase 列表中获取这个属性。
-
对,所以你的订阅中有这个。正如你所说,
console.log()打印正确......然后你将它分配给一个变量,然后你想要什么?您想在哪里使用该变量?在另一种方法?如果是这样,你什么时候调用其他方法? -
您似乎正在使用
angularfire2- 您能否分享/编辑您正在使用的angularfire2或@angular/fire版本的问题,以及更详细的代码清单? -
我必须将属性“condominio”的值作为字符串检索,这样我就可以使用它来循环另一个列表(名称为“condominio”)。
标签: angular typescript firebase firebase-realtime-database angularfire2