2017-05-11 109 views
0

越來越顯示作爲一名學生,我正在堅持努力創造以下來自我的Firebase數據庫的數據。數據未考慮

我有以下代碼:

  1. App.html

    <img id="profile-image" src="assets/profile/{{ photoURL }}" alt="{{ displayName }}"/> 
    <div id="profile-name" text-center class="width-full">{{ displayName }}</div> 
    
  2. App.component.ts

    var displayName; 
    var photoURL; 
    
    export class MyApp 
    { 
    
    constructor() 
    {  
        this.setUserData(); 
    

    }

    setUserData來()
    {
    this.uid = firebase.auth()。currentUser.uid;

    console.log("UID: "+ this.uid); 
    
    let ref = firebase.database().ref('/userProfile').child(this.uid); 
    
    ref.once('value', function(snapshot) 
    { 
        //CALLBACK 
        var key = snapshot.key; 
        var em = snapshot.child("/email").val(); 
        var nm = snapshot.child("/name").val(); 
        var dpn = snapshot.child("/displayName").val(); 
        var pURL = snapshot.child("/photoURL").val(); 
    
        //LOG 
        console.log("KEY: "+key); 
        console.log("NAME: "+nm); 
        console.log("DISPLAYNAME: "+dpn); 
        console.log("EMAIL: "+em); 
        console.log("PHOTOURL: "+pURL); 
    
        displayName = dpn; 
        photoURL = pURL; 
    
    }, function(error) 
    { 
        // The callback failed. 
        console.error(error); 
    }); 
    

在我的控制檯我看到UID是越來越加載,正確的數據越來越牽強, 但我似乎無法讓我的看法,結果呢?它只是空着。

有人可以解釋我失蹤或我應該做什麼?

在此先感謝!

回答

0

你應該聲明變量的類中,

export class MyApp{ 
displayName :string; 
photoURL :string; 
+0

如果我聲明類中的變量,並更改屬性this.displayName。我得到一個運行時錯誤:這是null。 (由於數據異步加載) – Vdonroberto

+0

你能正確地對齊你的代碼在問題 – Sajeetharan

+0

我試圖讓它對齊,但由於某種原因它分裂我的代碼。 – Vdonroberto