【问题标题】:ionic firebase get user information from real time databaseionic firebase 从实时数据库中获取用户信息
【发布时间】:2019-10-22 13:10:37
【问题描述】:

我试图在我的 Firebase 数据库中获取用户信息(例如:用户名),但在 .ts 文件中,我不知道如何对其进行编码

我厌倦了使用auth.uid 从数据库中获取用户名但没有用。下面是我用来保存用户配置文件的代码。效果很好。

this.afAuth.authState.pipe(take(1)).subscribe(auth => {
  let path= 'userProfile'+'/'+ auth.uid;
  this.afDatabase.object(path).set(this.profile);
})

但我不知道如何获取它的信息。所以在我的数据库中,在“userProfile”下有“用户名”。如何使用此逻辑获取用户名?我需要使用 auth.uid 在我的应用中显示用户名

【问题讨论】:

    标签: firebase ionic-framework firebase-realtime-database firebase-authentication ionic4


    【解决方案1】:

    您似乎有以下数据库:

    userProfile
          userid
            username : peter
    

    您可以执行以下操作:

    export class DataComponent implements OnInit{
      ref : any;
      constructor(db: AngularFireDatabase, public afAuth: AngularFireAuth) {
         ref = db.list('/userProfile', ref => ref.orderByKey().equalTo(this.afAuth.auth.currentUser.uid)).valueChanges();
        }
    
      ngOnInit(){
        ref.subscribe(items => {
           console.log(items);  
        });
      }
    
    }
    

    在这里查询节点userId,然后使用该节点可以检索username

    检查以下内容:

    https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md

    【讨论】:

    • auth 下有一条红线。它说找不到名称'auth'。有什么方法可以立即从 userProfile 中检索用户名?
    • 是的,在我的代码中我没有定义auth,您需要在构造函数中添加auth,然后检索uid
    • 检查编辑,如果要获取当前登录的用户名,则必须从顶部节点到用户名节点的路径
    • 是的,我可以看到它只是说 0.. 所以现在我必须在 userProfile/0 (这是唯一的用户 ID)/用户名中获取用户名,但我无法弄清楚
    • 在您的数据库中,您必须添加auth.uid,正如您在问题中所写的那样,然后要检索用户名,您会再次获得auth.uid,正如我在答案中所写的那样
    猜你喜欢
    • 2020-04-15
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2023-04-06
    相关资源
    最近更新 更多