Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Firestore Observables to Promises
Jonathan Gamble
Jonathan Gamble

Posted on • Edited on

     

Firestore Observables to Promises

Update 2/26/24

This post is out of date since Firebase 9. For latest Firebase posts, seeCode.Build.


I am crazy, so I created a quick reference. Let me know if I missed one!

Note:

afa = AngularFireAuth
afs = AngularFirestore

but should be same premise in any framework.

Docs

constfoo=awaitthis.afs.doc(`docPath`).valueChanges().pipe(take(1)).toPromise();
Enter fullscreen modeExit fullscreen mode

or

constfoo=(awaitthis.afs.doc('docPath').get().toPromise()).data();
Enter fullscreen modeExit fullscreen mode

or

constfoo=(awaitthis.afs.doc('docPath').get().pipe(take(1)).toPromise()).data();
Enter fullscreen modeExit fullscreen mode

or

constfoo=(awaitthis.afs.doc('docPath').snapshotChanges().pipe(take(1)).toPromise()).payload.data();
Enter fullscreen modeExit fullscreen mode

But the shortest is:

constfoo=(awaitthis.afs.doc('docPath').ref.get()).data();
Enter fullscreen modeExit fullscreen mode

Collections

constfoo=awaitthis.afs.collection('colPath').valueChanges().pipe((take(1))).toPromise();
Enter fullscreen modeExit fullscreen mode

or

constfoo=(awaitthis.afs.collection('colPath').snapshotChanges().pipe(take(1)).toPromise()).map((m)=>m.payload.doc.data());
Enter fullscreen modeExit fullscreen mode

or

constfoo=(awaitthis.afs.collection('colPath').get().toPromise()).docs.map((m)=>m.data());
Enter fullscreen modeExit fullscreen mode

or (overkill from last version)

constfoo=(awaitthis.afs.collection('colPath').get().pipe(take(1)).toPromise()).docs.map((m)=>m.data());
Enter fullscreen modeExit fullscreen mode

or

constfoo=(awaitthis.afs.collection('colPath').ref.get()).docs.map((m)=>m.data());
Enter fullscreen modeExit fullscreen mode

User

As you may have noticed...

constbar=awaitthis.afa.currentUser;
Enter fullscreen modeExit fullscreen mode

... does not load correctly in a lot of cases.

So try these:

constbar=awaitthis.afa.authState.pipe(take(1)).toPromise();
Enter fullscreen modeExit fullscreen mode

or (authstatechanged is most accurate)

constbar=awaitnewObservable((observer:any)=>{this.afa.onAuthStateChanged(observer)}).pipe(take(1)).toPromise();
Enter fullscreen modeExit fullscreen mode

or

constbar=awaitnewPromise((resolve:any,reject:any)=>this.afa.onAuthStateChanged((user)=>{user?resolve(true):resolve(false);},(e:any)=>reject(e)));
Enter fullscreen modeExit fullscreen mode

or (the shortest)

constbar=awaitthis.afa.user.pipe(take(1)).toPromise();
Enter fullscreen modeExit fullscreen mode

And anywhere you can usetake(1) you can usefirst() if you want to emit an error.

J

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

My main job is not in IT, but I do have a CS degree and have been programming for over 20 years (only born in 1984). I hate dealing with Servers, and want Graph Databases to be the new norm.
  • Location
    Louisiana
  • Education
    LSU, Oregon State, Middlebury
  • Joined

More fromJonathan Gamble

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp