angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

forEach works with snapshotChanges(), but not .map, even though the documentation says it should.

Open Metsuryu opened this issue 9 years ago • 21 comments

Version info

Angular: 4.2.4

Firebase: "firebase": "^4.5.0"

AngularFire: "angularfire2": "^5.0.0-rc.1"

Other (e.g. Ionic/Cordova, Node, browser, operating system): AngularCLI Project, Windows 7

How to reproduce these conditions


allUsers: AngularFirestoreCollection<any>;
users: Observable<any[]>;

//then in constructor:
this.allUsers = db.collection("users");
this.users = this.allUsers.snapshotChanges();

// This works:
this.users.forEach( user => {
  user.forEach( userData =>{
    let data = userData.payload.doc.data();
    let id = userData.payload.doc.id;
    console.log( "ID: ", id, " Data: " , data );
    });
});
// This doesn't work, when it should:
this.users.map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data();
    const id = a.payload.doc.id;
    console.log( "ID: ", id, " Data: " , data );
    return { id, data };
    });
});

Debug output

** Errors in the JavaScript console ** None.

Expected behavior

.map() should work.

Actual behavior

.map() doesn't work.

Metsuryu avatar Oct 05 '17 06:10 Metsuryu

.subscribe() also works as it should, and .map() works if inside a .subscribe(), like so:

  this.users.subscribe( data =>{
  	if (data) {
  		console.log("Subscribe: ", data);
  		data.map( test =>{
  			console.log("Map: ", test.payload.doc.data());
  		});  		
  	}
  })

Metsuryu avatar Oct 05 '17 06:10 Metsuryu

valueChanges returns an Observable, therefore you can do anything on it that you could on any other Observable. Therefore map works as expected as you can see here: https://stackblitz.com/edit/angular-template-firebase-uxldsj?file=app/app.module.ts

Toxicable avatar Oct 05 '17 06:10 Toxicable

@Toxicable I tried that code now, both with valueChanges and with snapshotChanges, and .map() still doesn't work for me.

Also, I didn't put <Type> in this.allUsers = db.collection("users"); before, but now that I did like this this.allUsers = db.collection<any>("users"); it still doesn't work.

But since it works in the link you provided, I guess it's a problem on my end, maybe I missed something.

Metsuryu avatar Oct 05 '17 06:10 Metsuryu

@Metsuryu What error do you get ?

Toxicable avatar Oct 05 '17 06:10 Toxicable

@Toxicable No errors, the console.log just doesn't print anything. I'm trying a few more things and I'll update this post later with what I find.

I don't know if firebase.database().enableLogging would show something, since I can't enable it, firebase.database().enableLogging(true); shows me an error saying enableLogging doesn't exist when I try it.

Metsuryu avatar Oct 05 '17 07:10 Metsuryu

@Metsuryu are you subscribing the second one anywhere?

sionfletcher avatar Oct 05 '17 07:10 sionfletcher

@sionfletcher No, should I? In the documentation there is no mention of having to subscribe it before using .map().

Edit: It does work if I subscribe right after I call .map(), thank you @sionfletcher I had no idea I had to do that.

Metsuryu avatar Oct 05 '17 07:10 Metsuryu

@Metsuryu I don't think the observable will emit any values until it's subscribed to, or you use users | async in your template.

sionfletcher avatar Oct 05 '17 07:10 sionfletcher

@sionfletcher hey man, i need ur help with something similar to this post, i am trying to return the id from the payload of the doc in a method but struggling to do so, let me know if u can help - [email protected]

sabeehshah avatar May 05 '18 19:05 sabeehshah

@sabeehshah show us the code.

Metsuryu avatar May 06 '18 03:05 Metsuryu

After upgrading to ^5.0.0-rc.9 it finally works like this:

this.tasksCollection.snapshotChanges().pipe(
        map(
            changes => { return changes.map(a => {
            const data = a.payload.doc.data() as Task;
            data.id = a.payload.doc.id;
            return data;
          });
          }
        )).subscribe();

fabianobando93 avatar May 20 '18 20:05 fabianobando93

@fobando93 the map still not working inside .pipe( map ), what i have to do?

leonardocoutinho avatar May 24 '18 10:05 leonardocoutinho

is working for me with the map inside the pipe() thanks i am in the last version of rxjs and firebase ( 11/06/2018)

import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';

import { Observable } from 'rxjs/Observable'; import { map } from 'rxjs/operators';

this.tasks = this.tasksCollection.snapshotChanges().pipe( map(changes => { return changes.map(a => { const data = a.payload.doc.data() as Task; data.id = a.payload.doc.id; console.log("hola"); return data; }); }));

i do that in my code

Gregorio19 avatar Jun 11 '18 22:06 Gregorio19

After upgrading to ^5.0.0-rc.9 it finally works like this:

this.tasksCollection.snapshotChanges().pipe(
        map(
            changes => { return changes.map(a => {
            const data = a.payload.doc.data() as Task;
            data.id = a.payload.doc.id;
            return data;
          });
          }
        )).subscribe();

FryannM avatar Dec 18 '18 03:12 FryannM

THANKS ITS WORK FOR ME , TE AMO :$

FryannM avatar Dec 18 '18 03:12 FryannM

After upgrading to ^5.0.0-rc.9 it finally works like this:

this.tasksCollection.snapshotChanges().pipe(
        map(
            changes => { return changes.map(a => {
            const data = a.payload.doc.data() as Task;
            data.id = a.payload.doc.id;
            return data;
          });
          }
        )).subscribe();

It took a while to find but this works

gifthlong avatar Feb 03 '19 00:02 gifthlong

@gifthlong I'm trying with latest version and it doesn't

erperejildo avatar Mar 21 '19 10:03 erperejildo

its not working even for me

hiranyey avatar Jun 06 '20 11:06 hiranyey

not working for me too.

himanshu-new avatar Jul 04 '21 17:07 himanshu-new

also trying to use "map" according to the docs and id doesnt work:

this.afs.collection('users').snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        console.log(data);
        console.log(id);
      }))
    );

Error: src/app/app.component.ts:48:7 - error TS2552: Cannot find name 'map'. Did you mean 'Map'?

48 map((actions: any[]) => actions.map(a => {

arielhasidim avatar Aug 19 '21 12:08 arielhasidim