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
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
Share Improve this question edited Jul 26, 2020 at 15:00 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Jul 26, 2020 at 10:40 Mohit KumarMohit Kumar 78013 silver badges43 bronze badges2 Answers
Reset to default 4Try 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();
}