Ad

Firebase Firestore - Use CollectionGroup Queries In Cloud Function

I want to use collectionGroup queries in a cloud function, and it only appears to be available if I import from firebase/app:

import * as firebase from 'firebase/app';
const db = firebase.firestore();

...
const snap = await db.collectionGroup('comments').where('foo', '==', 'bar').get();

But when I try to deploy I get

TypeError: firebase.firestore is not a function

In my other cloud functions, I have always imported from firebase-admin, but typescript tells me it has no collectionGroup method.

import * as admin from 'firebase-admin';
const db = admin.firestore();

Currently my versions are sitting at the following which should be latest:

"firebase": "^6.2.4",
"firebase-admin": "^8.2.0",

It would also be nice to understand why you would get your db instance from 'firebase-admin' or 'firebase/app'. I don't understand why there are 2 separate instances.

Ad

Answer

This should work:

import * as admin from 'firebase-admin'
const db = admin.firestore()

const snap = db.collectionGroup('comments').where('foo', '==', 'bar').get()

The collectionGroup type is supported on the last version

I would recommend to delete node_modules and install again (I just sanity check your sample and it went good, no issues on "firebase-admin": "8.0.0").

Ad
source: stackoverflow.com
Ad