2

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",
8
  • what does {{info}} displaying? Commented Sep 27, 2018 at 12:16
  • nothing at all, tried without async - differ Object in Object Commented Sep 27, 2018 at 12:18
  • 1
    Try this.back.getUploads().subribe((item: any) => {}) an observable have to be subribed. Guess it's a long day and you need some rest ;) Commented Sep 27, 2018 at 12:18
  • now I get undefined in console. Commented Sep 27, 2018 at 12:20
  • 1
    Changes map operation and it worked ! Commented Sep 27, 2018 at 12:22

1 Answer 1

2

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;
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.