Angular 6 Firebase快照返回未定义

时间:2018-09-27 12:14:07

标签: angular 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;
}

我的服务上传对象(工作正常):

busines = {} as Data;


createItemAuth() {
   this.afDatabase.list(`users/${this.auth.userId}/company/`).push(this.busines)
}

我的服务getUpload:

 getItem: Observable<any[]>;
 getUploads() {
    this.getItem = this.afDatabase.list(`users/${this.auth.userId}/company/`).snapshotChanges().pipe(map(items => {
      return items.map(a => {
        const data = a.payload.val();
        const $key = a.payload.key;
        const $ref = a.payload.ref;
        return { $key, ...data, $ref };
      });
    }));
    return this.getItem;
  }

在组件中调用它:

uploads: Observable<Data[]>;
ngOnInit() {
   this.uploads = this.back.getUploads();
   console.log(this.back.getUploads())
}                                                           

HTML :(浏览器完全没有)

<div *ngFor="let info of uploads | async">
  <p>{{info.name}}</p>
</div>

在ngOnInit()上的Console.log:

  

可观察{_isScalar:否,来源:可观察,操作员:   MapOperator}运算符:MapOperator {项目:ƒ,thisArg:未定义}   来源:可观察的{_isScalar:false,_subscribe:ƒ}   _isScalar:假   原始:对象

版本:

"rxjs": "^6.1.0",
  "firebase": "^5.4.1",
  "@angular/cli": "6.0.0",
    "typescript": "2.7.2",

1 个答案:

答案 0 :(得分:2)

我不得不更改以下代码:

来自此:

  getUploads() {
    this.getItem = this.afDatabase.list(`users/${this.auth.userId}/company/`).snapshotChanges().pipe(map(items => {
      return items.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 = this.afDatabase.list(`users/${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;
  }