Angular 2:从firebase中检索对象

时间:2017-11-13 15:05:42

标签: angular firebase firebase-realtime-database angularfire2

下面是我的firebase数据库截图: enter image description here

以下是“app.component”中的代码:

import { AngularFireDatabase, FirebaseListObservable } from "angularfire2/database-deprecated";
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import * as firebase from 'firebase/app';

@Component ....

items: FirebaseListObservable<any[]>;

constructor(public af: AngularFireDatabase) {
   this.items = af.list('/projects', {
     query: {
       limitToLast: 10
     }
   });
   console.log(this.items);
}

但我没有收到数据。有谁可以帮我解决这个问题?

我已完成angularfire2设置并能够将数据写入实时数据库。这是从db获取数据的问题。

1 个答案:

答案 0 :(得分:2)

您需要在列表上调用订阅方法。就这样:

this.items = af.list('/projects').valueChanges().subscribe(items => {
    console.log(items);
}));

我看到你正在使用database-deprecated包。代码应该是这样的:

this.firebaseList = af.list('/projects'); // This is of type FirebaseListObservable

this.items = this.firebaseList.subscribe(items => {
    console.log(items);
})); // This is of type subscription.

this.firebaseList.push({...data}); // Now you can push.