angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

docs: map function error

Open markgoho opened this issue 9 years ago • 3 comments

Version info

Angular: 4.4.4

Firebase: 4.5.0

AngularFire: 5.0 rc1

Other (e.g. Ionic/Cordova, Node, browser, operating system):

How to reproduce these conditions

copy .snapshotChanges() sample code into new project

Failing test unit, Plunkr, or JSFiddle demonstrating the problem (if needed)

Steps to set up and reproduce Copy docs code into new project

Sample data and security rules n/a <-- include/attach/link to some json sample data (or provide credentials to a sanitized, test Firebase project) -->

Debug output

** Errors in the JavaScript console **

** Output from firebase.database().enableLogging(true); **

** Screenshots ** image

Expected behavior

Expected the docs example to create an observable of Shirt[].

Actual behavior

Additional comments

Side note: payload.doc.id() -- id() isn't a method on doc, it's just a property. I can submit a PR for this separately, but this may be corrected in the correction of this issue 👍

markgoho avatar Oct 03 '17 21:10 markgoho

This is a quick fix. Would you like to send in the PR @markgoho? I'm tied at at the moment.

import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

export interface Shirt { name: string; price: number; }
export interface ShirtId extends Shirt { id: string; }

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let shirt of shirts | async">
        {{ shirt.name }} is {{ shirt.price }}
      </li>
    </ul>
  `
})
export class AppComponent {
  private shirtCollection: AngularFirestoreCollection<Shirt>;
  shirts: Observable<Shirt[]>;
  constructor(private readonly afs: AngularFirestore) {
    this.shirtCollection = afs.collection<ShirtId>('shirts');
    // .snapshotChanges() returns a DocumentChangeAction[], which contains
    // a lot of information about "what happened" with each change. If you want to
    // get the data and the id use the map operator.
    this.shirts = this.shirtCollection.snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as Shirt;
        const id = a.payload.doc.id;
        return { id, ...data };
      })
    });
  }
}

davideast avatar Oct 03 '17 22:10 davideast

I'm on it!

markgoho avatar Oct 03 '17 22:10 markgoho

I have tried this a thousand times in the last two days, and I continually get the ts errors that "The supplied parameters do not match any signature of call target"...

SGissubel avatar Feb 18 '18 22:02 SGissubel