2016-11-10 165 views
2

我遇到了一個問題,我試圖從db中獲取一些數據。我收到的數據,但我不能顯示它,因爲它是對象而不是字符串。我怎樣才能把這些對象變成字符串我已經嘗試了一些安靜的東西,但似乎沒有任何工作。無法從objectobservable獲取對象名稱

Component.ts

this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true}); 
    this.artistitems.subscribe(snapshot => { 
     this.artistvalues = snapshot.val(); 
     this.artistitemsid = this.artistvalues.releases; 
     console.log(this.artistitemsid) 
    }); 

在控制檯輸出從artistitemsid

aroundthefur:Object 
b-sides&rarities:Object 
diamondeyes(deluxe):Object 
gore:Object 
koinoyokan:Object 
saturdaynightwrist(explicitversion):Object 
thestudioalbumcollection:Object 

數據庫

database

一切,我儘量把我的component.html崩潰,我也實在沒有愛迪我怎麼能得到的值了對象,而不是獲取對象本身

時,我有這個在我component.html

<ul> 
    <li *ngFor="let item of artistitemsid"> 
     {{ item.value }} 
    </li> 
    </ul> 

我收到此錯誤

內聯模板:8:12原因:無法找到類型爲'object'的不同支持對象'[object Object]'。 NgFor僅支持綁定到陣列等陣元。

+0

是對象的數組內的對象?是這樣你需要迭代與* NgFor也張貼您的HTML組件 –

+0

我沒有發佈它的錯誤,當我刪除.value在HTML我收到相同的錯誤 –

+0

試試這個,並告訴我你得到

  • {{item | JSON}}
  • 回答

    2

    顯示藝術家版本中的按鍵列表:around thefur,b-sides &稀有程度等。

    所以,你可以從你的releases對象中提取這些按鍵:

    this.artistitems = af.database.object('/users/'+this.artistid, {preserveSnapshot: true}); 
    this.artistitems.subscribe(snapshot => { 
        this.artistvalues = snapshot.val(); 
        this.artistitemsid = Object.keys(this.artistvalues.releases); 
    }); 
    

    而且隨着ngFor結果數組在迭代:

    <ul> 
        <li *ngFor="let item of artistitemsid"> 
        {{ item}} 
        </li> 
    </ul> 
    
    +1

    它的工作! !我非常感謝你! –

    1

    從您的firebase結構artistitemsid中取回一個obeject {},您無法在對象上使用ngFor。

    但是,你應該能夠直接與插補訪問值:

    {{artistitemsid.gore.value}} 
    

    現在取決於價值的,如果它是對象的數組,然後你可以用ngFor

    遍歷它

    或者你可以拿你的價值觀和在數組中添加他們:

    你從火力得到的東西像

    {aroundthefur:{},b-sides&rarities:{},...} 
    

    this.artistitems.subscribe(snapshot => { 
         this.artistvalues = snapshot.val(); 
         this.artistitemsid = this.artistvalues.releases; 
    
         const myArray = []; 
         for (let key in this.artistitemsid){ 
          myArray.push(this.artistitemsid[key]); 
         } 
         this.items = myArray; // items is declared outside so you can use it on a ngFor 
    
         console.log(myArray) 
        }); 
    

    使用函數內部上ngOnInit,你會得到這樣的:

    items = [aroundthefur:{},b-sides&rarities:{},...] 
    

    而且如果我沒有記錯,你希望可以作爲

    <ul> 
        <li *ngFor="let item of items"> 
         {{ item.value }} // basically is aroundthefur.value 
        </li> 
        </ul> 
    
    +0

    0:對象 1:對象 2:對象 3:對象 4:對象 5:對象 6:對象 這是我在console.log(myArray)中的輸出現在xD –

    +0

    儘管我的webapp中有10個項目符號。所以它看到有10個,只是仍然沒有價值 –

    +0

    [「Image」](https://i.gyazo.com/4dd67374174c2d2c85db470f4d691b30.png) 她是我如何看到consolelog –