根据角度用户ID从数据库中获取用户角色 [英] get user role from database based on user id in angular
问题描述
我正在使用身份验证服务"来保留所有用户身份验证功能.当用户通过身份验证时,我获取用户的 id 并从数据库表中获取相关记录,但我无法获取角色"字段的值.我在构造函数中使用的代码是这样的:
I am using an "auth service" to keep all the user authentication functions. When the user is authenticated, I get the user's id and I fetch the relevant record from the database table, but I fail to get the value of the "role" field. The code I am using in the constructor is this:
constructor(
private _firebaseAuth: AngularFireAuth,
private db: AngularFireDatabase,
private router: Router) {
this.user = _firebaseAuth.authState;
this.user.subscribe(
(user) => {
if (user) {
this.userDetails = user;
this.userEmail = this.userDetails.email;
this.uid = this.userDetails.uid;
this.getUserRole(this.uid).subscribe(result => {
this.role = result.role;
console.log('role >>>>>>>>>>>>>', this.role);
});
console.log('uid >>>>>>>>>>>>>', this.uid);
console.log('role >>>>>>>>>>>>>', this.role);
}
else {
this.userDetails = null;
}
}
);
}
getUserRole(userId: string): Observable<any> {
return this.db.list('users', ref => ref.orderByChild('uid').equalTo(this.uid)).valueChanges()
.map(result => result[0]);
}
this.uid
具有正确的值,但 this.role
未定义.
this.uid
has the correct value, but this.role
is undefined.
我猜测是对数据库表的调用是异步的问题.我怎样才能得到这个值?
I suppose the problem that the call to the database table is asynchronous. How can i get this value?
推荐答案
我添加了一些代码来让你理解 observable 的回调.看看吧.
I have added some code to make you understand the callbacks of observable. Have a look at it.
this.currUser.subscribe(
(val) => {
// This is called as success callback
this.role = val[0].role;
// perform task using this.role. You should be writing your code here.
},
(error) => {
// This is called as error callback, It will be executed if any erorrs were thrown from `currUser()`
// log if there are any errors here
},
() => {
// This is called as complete block
// This will be executed after success callback or the error callback.
}
);
更新:
this.user.subscribe(
(user) => {
if (user) {
this.userDetails = user;
this.userEmail = this.userDetails.email;
this.uid = this.userDetails.uid;
this.getUserRole(this.uid).subscribe(result => {
this.role = result.role;
console.log('role >>>>>>>>>>>>>', this.role); // print log 1
});
// log 2 will be executed before log 1 this is due to asynchronous behaviour
console.log('role >>>>>>>>>>>>>', this.role); // print log 2
}
else {
this.userDetails = null;
}
}
);
日志 2 将在日志 1 之前执行,这是由于异步行为.如果您认为您的代码按行号顺序执行,那么您就错了.getUserRole
是异步方法.
log 2 will be executed before log 1 this is due to asynchronous behaviour. If you are thinking that your code executes sequentially with line numbers, then you are wrong. getUserRole
is asynchronous method.
您也可以使用 get
或 set
从其他组件访问 this.role
.无论你做什么都是合适的.
You can access this.role
from other components as well using get
or set
. Whatever you have done is appropriate.
你应该做的是获取 this.role
仅当它不是 undefined 时.
What you should be doing is fetch this.role
only if it is not undefined.
在您尝试访问 this.role
的外部组件中,首先检查 undefined
然后访问它.
In your external component where your are trying to access this.role
, check for undefined
first and then access it.
其他组件 .ts
if(this.role != undefined){
let role = this.role
}
演示:检查此处查看异步方法的工作原理
DEMO : Check here to see how asynchronous methods work
这篇关于根据角度用户ID从数据库中获取用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!