最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to receive data from real time database in firebase in angular - Stack Overflow

programmeradmin6浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

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();
}
发布评论

评论列表(0)

  1. 暂无评论