Firebase将数字读取为“ null”

时间:2018-10-09 02:02:52

标签: firebase ionic-framework firebase-realtime-database ionic3

我有一个正在使用Firebase实时数据库的离子应用程序。本质上,我使用Firebase对用户进行身份验证,并将用户选择输入存储在Firebase数组下。目前,我仍在使用firebase,并且昨天运行良好,但是今天突然间,根据chrome的控制台,firebase似乎将我的'id'值读为'null'。

代码引用了CrudFirebaseDetailPage,.ts在这里:

import { Component, ChangeDetectorRef } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
import { LoadingProvider, FirebaseDatabaseProvider } from '../../../../providers/providers';
import { IonicSelectableComponent } from 'ionic-selectable';
import { DiagnosisService } from '../../services';
import { Diagnosis } from '../../types';

@IonicPage()
@Component({
    selector: 'page-crud-firebase-detail',
    templateUrl: 'crud-firebase-detail.html',
})
export class CrudFirebaseDetailPage {
    private form: FormGroup;
    diagnosisControl: FormControl;
    diagnoses: Diagnosis[];
    action: number;
    data: any = { diagnosis : ''};
    items: any;

    constructor(public nav: NavController, private view: ViewController, private loading: LoadingProvider, private formBuilder: FormBuilder, private diagnosisService: DiagnosisService, public params: NavParams, public crud: FirebaseDatabaseProvider) {
        this.action = params.data.action;
        this.data = params.data && params.data.data || this.data;
        this.diagnoses = this.diagnosisService.getDiagnoses();
        this.diagnosisControl = this.formBuilder.control(this.diagnoses[0],
            Validators.required);
        this.form = this.formBuilder.group({
            id: [this.data && this.data.id],
            diagnosis: [this.data && this.diagnosisControl]
        });
    }

    diagnosisChange(event: {
        component: IonicSelectableComponent,
        value: any
    }) {
        console.log(event.value);
    }

    submit() {
        this.loading.present();
        this.crud.save(this.form.value);
        this.loading.dismiss();
        this.close();
    }

    close() {
        this.view.dismiss();
    }

}

代码在firebase-database.ts下也引用了FirebaseDatabaseProvicer:

import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';

@Injectable()
export class FirebaseDatabaseProvider {

    user: any = {};

  itemsRef:AngularFireList<any>;

    constructor(public afd: AngularFireDatabase,
        public afAuth: AngularFireAuth) {
        this.afAuth.authState.subscribe((res) => {
            if (res) this.user = res;
            this.itemsRef = this.afd.list(`users/${this.user.uid}/notes/`);
        })
  }

  get read() {
   return this.itemsRef;
  }

  save(datas) {
    console.log(datas);
    if (datas.id) {
      let tmp = datas.id; 
      console.log(datas);
      this.itemsRef.update(String(tmp), datas);
    }
    else {
      let id = new Date().getTime();
        datas.id = id;
      this.itemsRef.set(String(id),{
        id : id,
        diagnosis : datas.diagnosisControl
      });
    }
  }

  delete(id) {
    this.itemsRef.remove(String(id));
  }

}

基本上,当新数据添加到Firebase后端时,我期望的数组结果以及我昨天得到的结果是:

users
--uid
----notes
------datetimestamp
--------id: datetimestamp
--------diagnosis: {the diagnosis chosen by user}

但是,今天,由于某种原因,我收到控制台错误:

  

错误:Reference.set失败:第一个参数包含未定义的   属性“ users.uid.notes.datetimestamp.diagnosis”

从本质上讲,我在上述所有内容中都将uid替换为用户id字符串,并将datetimestamp替换为实际的数值结果,以方便理解。

控制台显示:

{id:空,诊断:“甲状腺功能减退”}

但是,当我扩展该值时,会看到以下内容: 诊断:“甲状腺功能减退” id:1539049001838

由于某种原因,即使我正在传递结果值,该ID也被报告为null。有什么想法我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

没关系,我知道了。我的参数表现不佳。我得到以下数组:

users
--uid
----notes
------undefined
--------id: datetimestamp
--------diagnosis: {the diagnosis chosen by user}

删除了参数,并将this.data.id = new Date().getTime();添加到CrudFirebaseDetailPage,它似乎可以正常工作。