根据用户ID从数据库中获取用户角色 [英] get user role from database based on user id in angular

查看:240
本文介绍了根据用户ID从数据库中获取用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用身份验证服务"保留所有用户身份验证功能.用户通过身份验证后,我获得了用户的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?

推荐答案

我添加了一些代码,以使您了解可观察的回调.看看吧.

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.

您也可以使用getset从其他组件访问this.role.无论您做什么都适合.

You can access this.role from other components as well using get or set. Whatever you have done is appropriate.

您应该做的是仅在未定义this.role时获取它.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆