angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.64k stars 2.2k forks source link

Proposal: Optionally return a document ID on `valueChanges()` when reading a single document #2109

Closed joeyaurel closed 3 years ago

joeyaurel commented 5 years ago

Thanks to #1973 and #1976 (@codediodeio 🔥) the newest version of AngularFire allows to use valueChanges() in a way that it also maps the ID of the document to the document data that is being returned in addition to the document data itself.

const tasks: Observable<Task[]> = this.firestore.collection<Task>('tasks').valueChanges({ idField: 'id' });

This currently only works when working with a collection() call.

It may also be nice to do the same when reading a single doc() like so:

const taskId = 'abcdefg';
const task: Observable<Task> = this.firestore.doc<Task>(`tasks/${taskId}`).valueChanges({ idField: 'id' });
jamesdaniels commented 5 years ago

I'm amenable to this, any interest in putting together a PR?

mjastrzebowski commented 5 years ago

Sounds like a great opportunity to start contributing :) On my way with the PR

geogramdotcom commented 5 years ago

We should also include in collectionGroup as well. Currently the documentation reads that idField can be used in valueChanges(), however it code currently expects 0 arguments

mjastrzebowski commented 5 years ago

@geogramdotcom Thanks for the comment, I thought that I checked that case too, but I might miss something. I will update PR if that's right

jimisdrpc commented 4 years ago

Is the suggestion above available? I mean, how observe a single document? I am struggling to figure out how do it and add an idToken when using observing a collection? I would be grateful if someone can answer my question in https://stackoverflow.com/questions/61355351/angular-and-firestore-firebaseerror-missing-or-insufficient-permissions-how-a

jamesdaniels commented 3 years ago

Should be addressed in 6.1.0-rc.1