AngularFire5 - Reference.update失败:第一个参数包含属性中的函数

时间:2017-12-05 09:24:56

标签: firebase firebase-realtime-database ionic3 angularfire5

我尝试将更改保存到Firebase数据库中的现有节点,但我在此处收到此错误:

  

Reference.update失败:第一个参数包含属性中的一个函数' matatu-list.-L-RMcqjnladFM5-V80b.payload.node_.children_.comparator _' with contents = function NAME_COMPARATOR(left,right){       return util_1.nameCompare(left,right);   }

我想根据其各自的键(通过navParams从另一页传递)来编辑项目。

这是我用来构建数据库的接口:

interface.ts

export interface Matatu {
    $key?: string;
    matNumberPlate: string;
    matSacco: string;
    matDriver: string;
    matAccessCode: string;
    matStatus: string;
    matTracker: string;
    matLocation: string;

    //Optionals
    payload?:any;
    key?:any;

}

用于更新记录的.ts.html代码:

.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireObject, AngularFireDatabase } from 'angularfire2/database';
import { Matatu } from '../../models/matatu/matatu.interface';
import { Subscription } from 'rxjs/Subscription';


@IonicPage()
@Component({
  selector: 'page-edit-ma3',
  templateUrl: 'edit-ma3.html',
})
export class EditMa3Page {

  matatuRef$: AngularFireObject<Matatu>;
  matatuAsync$: any;
  matatu = {} as Matatu;
  sub: Subscription;

  constructor(public navCtrl: NavController, public navParams: NavParams, private database: AngularFireDatabase) {

    const matKey = this.navParams.get('matKey');
    this.matatuRef$ = this.database.object(`matatu-list/${matKey}`);
    this.matatuAsync$ = this.matatuRef$.snapshotChanges();
    //console.log(matKey);

    this.sub = this.matatuAsync$.subscribe(
      matatu => this.matatu = matatu
    )
  }

  editMatatu(matatu : Matatu){
    this.matatuRef$.update(matatu);

    this.navCtrl.pop();
  }

  ionViewWillLeave(){
    this.sub.unsubscribe();
  }

}

.html

<ion-content>
  <ion-list>
    <ion-list-header>
      Matatu Details 
    </ion-list-header>

    <ion-item>
        <ion-label>Sacco</ion-label>
        <ion-input type="text" [(ngModel)]="matatu.matSacco"></ion-input>
    </ion-item>

    <ion-item>
        <ion-label>Driver</ion-label>
        <ion-input type="text" [(ngModel)]="matatu.matDriver"></ion-input>
    </ion-item>

    <ion-item>
          <ion-label> Access Code</ion-label>
          <ion-input type="password" [(ngModel)]="matatu.matAccessCode"></ion-input>
    </ion-item>

  </ion-list>
  <ion-list radio-group [(ngModel)]="matatu.matTracker">
    <ion-list-header>
      Preferred Tracking 
    </ion-list-header>

    <ion-item>
        <ion-label>GPS</ion-label>
        <ion-radio checked="true" value="GPS"></ion-radio>
    </ion-item>

    <ion-item>
        <ion-label>Driver's Location</ion-label>
        <ion-radio value="Driver's Location"></ion-radio>
    </ion-item>
  </ion-list>

  <div padding>
    <button ion-button block (click)="editMatatu(matatu)">Save Changes</button>
  </div>
</ion-content>

我该怎么做才能纠正这个问题?如果向我指出我哪里出错了,以及其他方法可以做到这一点(即使是肮脏的那些!),我也会感激不尽。

1 个答案:

答案 0 :(得分:1)

您可以在占位符变量中传递密钥,或者您可以像这样连接它:

this.matatuRef$ = this.database.object(`matatu-list/`+ matKey);

请记住,这不是一个好方法,但它会做到这一点。

当您尝试推送包含函数或数据类型而非数据值的数据时,Firebase会触发此错误。

根据您的界面以及您从模板传递的值,您的身份 更新方法应该是这样的:

editMatatu(matatu : Matatu){

    this.matatuRef$.update({
      matDriver: this.matatu.matDriver,
      matAccessCode: this.matatu.matAccessCode,
      matSacco: this.matatu.matSacco
    });


  }