如何使用AngularFire2 FirebaseObjectObservale保存或推送到observable

时间:2016-12-10 18:40:36

标签: angular firebase firebase-realtime-database rxjs angularfire2

出于某种原因,我无法绕过angularfire2和firebaseobjectobservables和rxjs中的方法。我尝试使用$ save()但我得到一个错误,它不存在于firebaseobjectobservables上。我也试过.push(),这也引起了一个错误。我没有进口东西吗?

import {AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import {Observable} from 'rxjs/Observable';

  private Room:FirebaseObjectObservable<any>;


 constructor(private af:AngularFire, private http:Http) {
  this.Room=af.database.object(`/Rooms/1`);
}

  //This is where the error pops up
 this.Room.push({name:abc,room:1}).then((item) => { console.log(item.key); });

  //Here is my other version trying to use firbase method $save() w/ the same error
  this.Room.$save(this.playerO).then(_ => console.log("Pushed"));

1 个答案:

答案 0 :(得分:0)

试试这个。您必须将房间项目推送到房间列表,房间钥匙将自动生成。

import { AngularFire, FirebaseListObservable } from 'angularfire2';

export class Room {
   private Rooms:FirebaseListObservable<any[]>;

   constructor(private af:AngularFire) {
     this.Rooms=af.database.list('/Rooms');
     this.Rooms.push({name:'abc'}).then((room) => { console.log(room.key); });
   }
}