Javascript Can';t检查firebase中是否存在具有角度[离子]的轮廓
请,我需要帮助,我正在尝试检查Firebase数据库中是否存在带有Angular(Ionic)的用户配置文件。因此,如果它存在,则转到TabsPage,如果不存在,则转到EditProfiePage 但它不起作用。它返回空值!并始终转到EditProfile页面,请检查以下代码:Javascript Can';t检查firebase中是否存在具有角度[离子]的轮廓,javascript,angular,firebase,ionic-framework,Javascript,Angular,Firebase,Ionic Framework,请,我需要帮助,我正在尝试检查Firebase数据库中是否存在带有Angular(Ionic)的用户配置文件。因此,如果它存在,则转到TabsPage,如果不存在,则转到EditProfiePage 但它不起作用。它返回空值!并始终转到EditProfile页面,请检查以下代码: this.data.getProfile(<User> event.result).subscribe(profile =>{ console.log(profile); if (profile.
this.data.getProfile(<User> event.result).subscribe(profile =>{
console.log(profile);
if (profile.hasOwnProperty('$value') && !profile['$value'] )
{
this.navCtrl.setRoot("TabsPage")
}
else {
this.navCtrl.setRoot("EditProfilePage");
}
如图所示:
在您的数据提供程序中
而不是
返回这个.profileObject.snapshotChanges().pipe(first())代码>
你可以试试
返回此.profileObject.valueChanges()代码>无需检查对象是否具有属性。一个简单的if语句就足够了
public getProfile(user: User){
this.profileObject = this.database.object(`profiles/${user.uid}`);
return this.profileObject.valueChanges();
}
if语句检查配置文件是否存在(如果不存在,则结果为未定义或null)
谢谢你的回答
我用以下方法解决了这个问题:
1-我导入订阅,然后声明订阅类型的“authenticatedUser$”变量,然后声明用户类型的“authenticatedUser”变量(firebase用户)。-“login.ts”
2-在构造函数中,我将在'authenticatedUser$'变量中获取当前已验证用户,然后将其订阅到'authenticatedUser'变量-“AuthProvider.ts”
3-最后一件事,我将authenticatedUser var作为参数发送给我的数据提供者,并且我将订阅返回的值。-“DataProvider.ts”
授权提供者:
public getProfile(user: User){
this.profileObject = this.database.object(`profiles/${user.uid}`);
return this.profileObject.snapshotChanges().pipe(first());
}
import { LoginResponse } from '../../models/login-response';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import { Account } from '../../models/account';
....................
constructor(public auth:AngularFireAuth) {
console.log('Hello AuthProvider Provider');
}
public getAuthenticatedUser(){
return this.auth.authState;
}
数据提供者:
public getProfile(user: User){
this.profileObject = this.database.object(`profiles/${user.uid}`);
return this.profileObject.snapshotChanges().pipe(first());
}
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireObject, AngularFireList } from 'angularfire2/database';
import { User } from 'firebase/app';
import { Profile } from '../../models/profile';
................
profileObject: AngularFireObject<any>;
constructor(public database: AngularFireDatabase) {
}
public getProfile(user: User){
this.profileObject = this.database.object(`profiles/${user.uid}`);
return this.profileObject.valueChanges();
}
Hossam你在使用Firebase Cloud Firestore还是实时数据库?你好,guardezi,我在使用实时数据库。同样的事情。它说:“无法读取null的属性‘hasOwnProperty’”。也是一样。上面写着“空”。尽管配置文件存在。()@HossamBrgoth你能不能控制台.log()配置文件/user.uid?@HossamBrgoth不是这样的console.log(`profiles/${user.uid}`)
这将告诉您正在传递的参数在该点是否实际存在
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { LoginResponse } from '../../models/login-response';
import { DataProvider } from '../../providers/data/data';
import { User } from 'firebase/app';
import { Subscription } from 'rxjs/Subscription';
import { AuthProvider } from '../../providers/auth/auth';
................
user = {} as User;
private authenticatedUser$: Subscription;
private authenticatedUser: User;
constructor(public navCtrl: NavController,
public toast:ToastController, public navParams: NavParams, public data: DataProvider,
private myAuthProvider:AuthProvider) {
this.authenticatedUser$ = this.myAuthProvider.getAuthenticatedUser()
.subscribe((user: User)=>{
this.authenticatedUser = user
})
this.data.getProfile(this.authenticatedUser).subscribe(profile =>{
console.log(profile);
if (profile) {
this.navCtrl.setRoot("TabsPage")
} else {
this.navCtrl.setRoot("EditProfilePage");
}
});
}