我正在尝试使用Angularfire2更新Angular2项目中的
FirebaseListObservable对象中的项目. FirebaseListObservable的定义如下:
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>; }
@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); } }
houses $是FirebaseListObservable,house是列表中的项目. House是一个如下所示的对象:
{ name: "Mrs. Friendly",notes: "Watch out for fence!",street: "2530 Adams Ave.",$key: "-KKQWsf26apDiXZNExZd" }
当我尝试保存/更新现有项目时,出现以下错误:
Uncaught Error: Firebase.update Failed: First argument contains an
invalid key ($key) in path /$key. Keys must be non-empty strings and
can’t contain “.”,“#”,“$”,“/”,“[“,or “]”
我已经尝试将原始对象放在第一个参数中,将完整的URL放在那里,几乎每个组合都可以想到.根据Observable的定义,_ref也应该在那里工作.我也尝试从要存储的数据中提取密钥,似乎没有任何工作.似乎唯一有效的方法是完全删除更新行并使用以下内容:
this.af.database.object('/houses/'+house.$key).set(house);
我很好奇我应该放在第一个参数中,以便更新方法可以工作.
我正在使用Angular 2 rc-1,firebase 3.0.5和angularfire 2.0.0-beta.1