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.
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"
).
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error