简体   繁体   中英

How to save or push to observable with AngularFire2 FirebaseObjectObservale

For some reason I cannot wrap my head around the methods in angularfire2 and firebaseobjectobservables and rxjs. I tried using $save() but I get an error about it not existing on firebaseobjectobservables. I also tried .push() which also brought up an error. Am I not importing something?

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"));

Try this. You have to push room item to your room list and room key will be autogenerated.

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); });
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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