Angular 6 Firebase数据库无法获取快照

时间:2018-09-27 08:50:00

标签: typescript firebase firebase-realtime-database angular6

我这样上传到数据库:

我要上传的对象:

      export class Data {
          $key: string;
          name: string;
          address: string;
          address2: string;
          pscode: string;
          ccode: string;
          name2: string;
          trucks: Trucks;
          trailers: Trailers;
        email: string;
        phone: string;
        city: string;
        country: string;
        }

**Upload service :** 

  getItem: Observable<Data[]>;
key: string;
  busines = {} as Data;



   createItemAuth() {
this.afAuth.authState.subscribe(auth => {
  this.afDatabase.list(`users/${this.auth.userId}/company`).push(this.busines)
    });
}


//this not works : 

  getUploads() {
    this.getItem = this.afDatabase.list(`profile/${this.auth.userId}/company/`).snapshotChanges().map((actions) => {
      return actions.map((a) => {
        const data = a.payload.val();
        const $key = a.payload.key;
        const $ref = a.payload.ref;
        return { $key, ...data, $ref };
      });
    });
    return this.getItem;
  }

getUploads() this.getItem上我得到了:

  

输入'{$ ref:引用; $ key:字符串; } []'无法分配给类型   '数据[]'。       类型'{$ ref:参考; $ key:字符串; }”不可分配给“数据”类型。         类型'{$ ref:引用;属性'名称'丢失。 $ key:字符串; }'。

这有什么问题?

1 个答案:

答案 0 :(得分:-1)

另一种方法。

import * as firebase from 'firebase/app';
import { map } from 'rxjs/operators';
import { Observable, Subscription } from 'rxjs';

public mydata: Observable<any>;
public myarray = any[];

firebase.database().ref('/mypath').once('value').then(function(snapshot) { return snapshot.val(); })
      .then(items => {

        var icount = 0;
        this.mydata =[];
        this.myarray = [];

        this.mydata = items;

        for (let i in this.mydata) {

         console.log(this.mydata[i].key)
         console.log(this.mydata[i].value)
         console.log(this.mydata[i].value.something)

         this.myarray[icount] = [];
         this.myarray[icount]['something'] = this.mydata[i].val.something;

         icount++;

        }

      });

    } catch (e) {
      console.error(e);
    }