简体   繁体   中英

how to receive data from real time database in firebase in angular

I want to receive data from the realtime database in firebase. The Data is shown below: 在此处输入图像描述

The code I tried is

return this.db.list('messages', ref => {
  return ref.limitToLast(25).orderByKey();
});

but it's not working

Try this to get the messages list from the firestore. Import AngularFireDatabase service and AngularFireList to store message collection.

 import { Injectable } from '@angular/core'; import { AngularFireDatabase, AngularFireList } from '@angular/fire/database'; @Injectable({ providedIn: 'root' }) export class MessageService { messages: AngularFireList<any>; constructor(private http: HttpClient, private db: AngularFireDatabase) { } /** * Get All tickers from firebase */ getMessages(): Observable<any> { this.messages = this.db.list('messages'); return this.messages.valueChanges(); } }

Make sure you have imported AngularFireModule and AngularFireDatabaseModule in your app module.

AngularFire is build with the Observable Pattern via RxJs Library.

You can stream the data with the valueChanges() method, as described in the documentation

items: Observable<any[]>;
constructor(firestore: AngularFirestore) {
   this.items = firestore.collection('items').valueChanges();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM