所以我认为当用户登录时它会转到TabsPage但如果没有登录则转到登录页面。问题是我有两个用户是客户和活动组织者。所以我想做if(user.useType ===“customer”)转到TabsPage和if(user.useType ===“eventorganiser”)然后去EventTabsPage,否则用户转到LoginPage。
我正在尝试用户firebase快照但根本不工作,因为总是返回true。我接近了吗?我在数据库中有userType,它是userType:“customer”或userType:“eventorganiser”
import { Reference } from '@firebase/database-types';
@Component({
templateUrl: 'app.html',
})
export class MyApp {
rootPage: any;
public type : Reference
constructor(platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen) {
firebase.initializeApp(firebaseConfig);
this.type = firebase.database().ref("userProfile");
this.type.once("value")
.then(function (snapshot) {
this.type = snapshot.val().userType
});
const unsubscribe: Unsubscribe = firebase
.auth()
.onAuthStateChanged(user => {
if (user && this.type.toString() === "customer") {
this.rootPage = 'TabsPage'
unsubscribe();
}
else if (user && this.type.toString() === "eventorganiser") {
this.rootPage = 'EventTabsPage';
unsubscribe();
}
else {
this.rootPage = 'LoginPage';
unsubscribe();
}
});
数据库如下所示
{
"userProfile" : {
"3RQOUe9cqcRX2IMy0S290S9trSM2" : {
"birthDate" : "2018-02-20",
"email" : "acs@acs.com",
"firstName" : "asda",
"lastName" : "dsad",
"userType" : "customer"
},
"CHtJTfDmCgfaMY91ve4oepvIkuc2" : {
"birthDate" : "2018-02-20",
"email" : "liam@liam.com",
"firstName" : "lk",
"lastName" : "",
"userType" : "eventorganiser"
},
我所有用户的工作代码都在
之下 const unsubscribe: Unsubscribe = firebase
.auth()
.onAuthStateChanged(user => {
if (user) {
this.rootPage = 'TabsPage';
unsubscribe();
} else {
this.rootPage = 'LoginPage';
unsubscribe();
}
})
答案 0 :(得分:0)
有可能在您的应用调用时:
firebase
.auth()
.onAuthStateChanged(user => {
if (user && this.type.toString() === "customer") {
this.rootPage = 'TabsPage'
unsubscribe();
}
:
this.type = firebase.database().ref("userProfile");
this.type.once("value")
.then(function (snapshot) {
this.type = snapshot.val().userType
});
尚未就绪,因为它是异步调用,并且在调用onAuthStateChanged()
函数之前不会等待返回值。
另外,请确保这是正确的引用,因为/ userType是一个通用节点。
以下是我过去的表现:
const unsubscribe: Unsubscribe = firebase
.auth()
.onAuthStateChanged(user => {
if (user) {
firebase.database().ref("userProfile").once("value").then(function (snapshot) {
if (snapshot.val().userType === 'customer') {
this.rootPage = 'TabsPage';
} else {
this.rootPage = 'EventTabsPage'
}
});
} else {
this.rootPage = 'LoginPage';
unsubscribe();
}
});
在该流程之后,它首先获取用户,然后获取用户配置文件,然后检查值。