2017-01-02 5 views
0

イオン2のtsファイルでAngularFire2リファレンスをどのように使用できますか? (例えば、私は私のFirebaseへの参照を持っている)イオン2はtsファイルにAngularFire2リストを使用

this.courses=this.af.database.list('/CoursesList/2017/Software/A/SemA'); 

私は(ないHTMLファイル内)のTSファイルで反復処理をチェックするために、そのデータで何かをしたいです。出来ますか?

私はそれを実行しようとしましたが、私は、これは私のコード

this.courses 
    .subscribe(snapshots => { 
    snapshots.forEach(snapshot => { 
     console.log(snapshot.val()); 


    }); 

答えて

1

うんでエラー enter image description here

を入手!そのようにするには複数の方法があります。 .list()を呼び出すとObservableであるFirebaseListObservableが返され、.push(),.set(),.update().delete()などの便利なメソッドが返されます。

最も簡単な方法は、.subscribe()を呼び出して実際の配列を反復することです。

af.database.list('/songs').subscribe(songs => { 
    // songs is the downloaded array 
    // type could simply be any[] or whatever model you need 
    this.songs = songs; 
    // or you can forEach or whatnot 
    this.songs.forEach(song => console.log(song)); 
}); 

より有利な方法は.map()のようなObservable演算子を使用することです:あなたがが.map()オペレーターをインポートするか、それは動作しませんを確認してください

af.database.list('/songs') 
// Almost like a regular array map, but it emits for each item 
// added over time 
.map(song => { 
    song.name = song.name.toUpperCase(); 
    return song; 
}) 
.subscribe(songs => { 
    // songs is the downloaded array 
    this.songs = songs; 
    // All the names will be in caps 
    this.songs.forEach(song => console.log(song.name)); 
}); 

を!

import 'rxjs/add/operator/map` 
+0

すばらしい説明!私は自分の投稿を編集しました、なぜ間違っているのでしょうか? –

+0

はいAngularFireはデフォルトでスナップショットをアンラップするので、 '.val()'を呼び出す必要はありません。パラメータ名は、実際のP​​OJOで、 'DataSnapshot'ではなく' snapshots'であってはなりません。 –

+0

ここ//曲はダウンロードされた配列です this.songs = songs; 、ダウンロードした配列はどのようなタイプですか?それを宣言する方法? –

関連する問題