Angulandy2
Angulandy2

Reputation: 806

Angular 6 firebase snapshot returns undefined

I am uploading object to my database and then trying to retrieve all items. On the second step I get errors. :

My object class :

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;
}

My service upload object (works fine) :

busines = {} as Data;


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

My service 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;
  }

Calling it in component :

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

HTML: (nothing at all at browser)

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

Console.log on ngOnInit() :

Observable {_isScalar: false, source: Observable, operator: MapOperator} operator: MapOperator {project: ƒ, thisArg: undefined} source: Observable {_isScalar: false, _subscribe: ƒ} _isScalar: false proto: Object

Versioning :

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

Upvotes: 2

Views: 829

Answers (1)

Angulandy2
Angulandy2

Reputation: 806

I had to change the following code :

From this :

  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;
  }

To this :

 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;
  }

Upvotes: 2

Related Questions