【问题标题】:Angular 6 Firebase database can't get snapshotAngular 6 Firebase 数据库无法获取快照
【发布时间】:2019-03-03 03:12:39
【问题描述】:

我这样上传到数据库:

我要上传的对象:

      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:字符串; }'。

这有什么问题?

【问题讨论】:

标签: typescript firebase firebase-realtime-database angular6


【解决方案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);
    }

【讨论】:

  • 请给 Angulandy2 链接
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 2021-06-07
  • 2017-07-27
  • 2019-03-03
  • 1970-01-01
  • 2019-05-07
相关资源
最近更新 更多