代码之家  ›  专栏  ›  技术社区  ›  Tree55Topz

从FireBase检索用户信息并在应用程序中显示

  •  1
  • Tree55Topz  · 技术社区  · 7 年前

    import { Component, OnInit, ViewChild } from '@angular/core';
    import { IonicPage, NavController, NavParams } from 'ionic-angular';
    import { HomePage from '../../../pages/home/home';
    import { AngularFireDatabase } from 'angularfire2/database';
    import { Profile } from '../../../models/profile';
    import { AngularFireAuth } from 'angularfire2/auth';
    
    
    @Component({
      selector: 'page-profile',
      templateUrl: 'profile.html',
    })
    export class ProfilePage implements OnInit {
    
      name: string = "";
      hometown: string = "";
    
      constructor(private afDatabase: AngularFireDatabase, private afAuth: AngularFireAuth, public navCtrl: NavController, private navParams: NavParams) {
    
      }
    
      ngOnInit(){
    
      }
    
      onGoToHome(){
        this.navCtrl.push(HomePage)
      }
    
      retrieveUserData(){
        var userId = this.afAuth.app.auth().currentUser.uid;
    
    
      }
    
    }
    

    this 医生,但是语法对我不起作用。无法识别任何语法。我知道这是非常微不足道的。有人能帮我举一个清楚的例子来说明如何正确地获取和使用这些数据吗?FireBase中的表称为“配置文件”。

    1 回复  |  直到 6 年前
        1
  •  1
  •   JeremyW    7 年前

    documentation here

      this.afDatabase.database.ref(`profile/${userId }`).once('value').then( userDetailsAsObject => {
    
        let userDetails = userDetailsAsObject.val() ? userDetailsAsObject.val() : {};
        console.log(userDetails);
    
      }).catch( err => { 
        console.log('Error pulling /profile table inside functionName() inside componentName.component.ts'); 
        console.log(err); 
      });
    

    在评论中编辑每个附加问题:

    profile
       |
       |--12bd02nasBD0Bd93nbD    (userID)
              |
              |--name:"John Smith"
              |
              |--hometown:"Paris, France"
    

    12bd02nasBD0Bd93nbD 节点作为一个大对象…因此,您将以与任何其他JavaScript对象相同的方式访问该对象的属性。( dot notation or bracket notation

    this.name = userDetails['name'];
    this.hometown = userDetails['hometown'];
    

    name hometown