Posts

Showing posts with the label google-cloud-firestore

Angular / Firestore - What's the best way to check the User ID in multiple components?

Angular / Firestore - What's the best way to check the User ID in multiple components? I have an 'authservice.ts' file where I try to manage user auth style things. However, in each of my components, I'm always subscribing to the user to get the user ID. The user ID is then used inside my Firestore functions to query data where the user ID is referenced inside collection and document paths. Is there a more 'fluent' way of achieving the same thing? Or is this approach actually suitable? Below are the examples of the auth service and a component file that references the auth service. auth-service.ts ... user: Observable<firebase.User>; ... constructor() { this.user = afAuth.authState; } Any One Of My Components.ts ... private _subscription: Subscription; ... ngOnInit() { this.subscribeToUser(); } subscribeToUser() { this._subscription = this.authService.user.subscribe(user => { if (user) { this.userID = user.uid; th...

angularfire2 collection querying with endAt wont give result

angularfire2 collection querying with endAt wont give result I am trying to querying date range at firebase by using startAt() and endAt() methods.(Angular6) For date picking i use angular material MatDatepicker module. q_startDate; q_endDate; getStartDate(event: MatDatepickerInputEvent<Date>) { this.q_startDate = event.value.toISOString();} I defined q_startDate and q_endDate veriables and pushed data with getStartDate and getEndDate methods. const myCollection = this.afs.collection<objInterface>('collection_name', ref=> { let query: firestore.CollectionReference | firestore.Query = ref; query = query.orderBy('date_field', 'desc'); query = query.startAt(this.q_startDate); query = query.endAt(this.q_endDate); return query; }); In this query when i comment query = query.endAt(this.q_endDate); it works. How can i fix this issue, i want to include query also q_endDate? By click...

In Firestore, how can you do a compound query involving a key in a map without creating an index for every key?

In Firestore, how can you do a compound query involving a key in a map without creating an index for every key? In Firestore, how can you do a compound query involving a key in a map without creating an index for every key? For example, consider a collection which holds blog posts, and each blog post has categories. Post { title: .. ... categories: { cats: true puppies: true } } In order to query posts in a particular category in a paginated way, we would do something like this: let query = db.collection(`/posts`) .where(`categories.${categoryId}`, '==', true) .orderBy('createdAt') .startAfter(lastDate) .limit(5); But it seems that this would require a composite index ( categories.<categoryId> and createdAt ) for every single category. Is there any way around this? categories.<categoryId> createdAt In my case, it isn't feasible to create composite indices for every category since categories are user-gene...

Firestore can't get empty docs

Firestore can't get empty docs I have collection of documents where the id of the doc is the users id. Lets call these user documents. Each "user document" contains a subcollection of chat messages. But not all "user documents" contains any fields (data, other than the subcollection). I wan't to return all the doc in the collection that don't have any fields, but have a subcollection, but I seems this is not possible? var allUserDocs = {}, count = 0, users = firestore.collection("users"); users.get().then(snapshot => { snapshot.forEach(doc => { count++; allUserDocs[count] = doc.data(); }); allUserDocs.count = count; res.status(200).send(allUserDocs); }) this code only returns the docs that contains fields, not the docs that only have a subcollection? Is there a way to return all? How can i get a list of all document ids in the collection? both empty and non-empty ones? ...