Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use a query in new modular firestore #3203

Answered by gustavo-alarcon
m8xp0w3r asked this question in Q&A
Discussion options

Hello everyone,
I am moving from compat to modular firestore version. I have the following function with an optional query argument:

getCollection<T>(collectionName: string, queryFn?: QueryFn): AngularFirestoreCollection<T> {
 return this.firestore.collection<T>(collectionName, queryFn);
 }

With the modular version I probably will go like this:

getCollection(collectionName: string, queryFn?: QueryFn): CollectionReference<DocumentData> {
 return collection(this.firestore, collectionName);
 }

Unfortunately I did not find anything how to pass my (optional) Query here. Do you have any suggestions how I I can do this?

Regards Nils

You must be logged in to vote

Hi there !

I use the following approach, perhaps it could help you.

getUsers(): Observable<User[]> {
 const usersCollection: CollectionReference<User> = collection(
 this.firestore,
 'users'
 ) as CollectionReference<User>;
 return collectionData<User>(
 query(usersCollection, orderBy('createdAt', 'desc')),
 {
 idField: 'uid',
 }
 );
 }

Have a good day buddy !

Replies: 1 comment 1 reply

Comment options

Hi there !

I use the following approach, perhaps it could help you.

getUsers(): Observable<User[]> {
 const usersCollection: CollectionReference<User> = collection(
 this.firestore,
 'users'
 ) as CollectionReference<User>;
 return collectionData<User>(
 query(usersCollection, orderBy('createdAt', 'desc')),
 {
 idField: 'uid',
 }
 );
 }

Have a good day buddy !

You must be logged in to vote
1 reply
Comment options

Awesome. This helped me a lot. My biggest problem was, that my IDE (PHP Storm) did not import all functions and types correct (some were imported from compat, others were not automatically detected).

Answer selected by m8xp0w3r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /