Updating an item in FirebaseListObservable using AngularFire 2

I am trying to update an element in a FirebaseListObservable object in an Angular2 project using Angularfire2. The definition for FirebaseListObservable is as follows:

export declare type FirebaseOperation = string | firebase.database.Reference | firebase.database.DataSnapshot | AFUnwrappedDataSnapshot; export declare class FirebaseListObservable<T> extends Observable<T> { _ref: firebase.database.Reference | firebase.database.Query; constructor(_ref: firebase.database.Reference | firebase.database.Query, subscribe?: <R>(subscriber: Subscriber<R>) => Subscription | Function | void); lift<T, R>(operator: Operator<T, R>): Observable<R>; push(val: any): firebase.database.ThenableReference; update(item: FirebaseOperation, value: Object): firebase.Promise<void>; remove(item?: FirebaseOperation): firebase.Promise<void>; _checkOperationCases(item: FirebaseOperation, cases: FirebaseOperationCases): firebase.Promise<void>; } 

I have a service for showing, saving, deleting and updating houses, which look like this:

 @Injectable() export class HousesService { private _houses$:FirebaseListObservable<IHouse[]>; constructor(private af:AngularFire) { this._houses$ = this.af.database.list('/houses'); } save(house:IHouse) { if (house.hasOwnProperty('$key')) { this._houses.update('/houses/'+house.$key).set(house); } else { this._houses$.push(house); } } remove(id:any) { this._houses$.remove(id); } } 

where houses$ is FirebaseListObservable and house is an item from the list. A house is an object that looks like this:

 { name: "Mrs. Friendly", notes: "Watch out for fence!", street: "2530 Adams Ave.", $key: "-KKQWsf26apDiXZNExZd" } 

When I try to save / update an existing item, I get the following error:

Error during collection: Firebase.update failed: the first argument contains an invalid key ($ key) in the path / $ key. Keys must be non-empty strings and cannot contain ".", "#", "$", "/", "[" Or "]"

I tried putting the source object in the first parameter by posting here the full URL and almost every combination you might think of. According to the definition of Observable, _ref should also work there. I also tried to pull the key from the data that needs to be saved, and nothing works. The only thing that works is to completely remove the update line and use the following:

 this.af.database.object('/houses/'+house.$key).set(house); 

I'm curious what I had to put in the first parameter for the update method to work.

I am using Angular 2 rc-1, firebase 3.0.5 and angularfire 2.0.0-beta.1

+5
source share
2 answers

Your version of Angularfire is a bit older than mine, but for your save method, I think you can just pass the house key to save (house. $ Key) along with the object containing the updated data.

 this._houses.update(house.$key, house); 

I think that passing the '/ houses /' part causes an error because the $ key cannot begin with '/'.

+4
source
 var key = house.$key delete house.$key this._houses.update(key, house) 
0
source

All Articles