简体   繁体   English

如何从 angular 中的 firebase 中的实时数据库接收数据

[英]how to receive data from real time database in firebase in angular

I want to receive data from the realtime database in firebase.我想从 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.试试这个从firestore获取消息列表。 Import AngularFireDatabase service and AngularFireList to store message collection.导入AngularFireDatabase服务和AngularFireList来存储消息集合。

 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.确保您已在应用模块中导入AngularFireModuleAngularFireDatabaseModule

AngularFire is build with the Observable Pattern via RxJs Library. AngularFire 是通过 RxJs 库使用可观察模式构建的。

You can stream the data with the valueChanges() method, as described in the documentation您可以使用 valueChanges() 方法 stream 数据,如文档中所述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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