加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用AngularFire 2更新FirebaseListObservable中的项目

发布时间:2020-12-17 10:21:15 所属栏目:安全 来源:网络整理
导读:我正在尝试使用Angularfire2更新Angular2项目中的 FirebaseListObservable对象中的项目. FirebaseListObservable的定义如下: export declare type FirebaSEOperation = string | firebase.database.Reference | firebase.database.DataSnapshot | AFUnwrapp
我正在尝试使用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

您的Angularfire版本比我的版本略长,但是对于您的保存方法,我认为您可以通过房子的密钥保存(house.$key)以及包含更新数据的对象.
this._houses.update(house.$key,house);

我认为通过传递’/ houses /’部分,错误被抛出,因为$key不能以’/’开头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读