TypeError: db.collection is not a function Firestore
I was presented this error in an attempt to import Firestore from the Firebase Admin SDK. Can't find anything that helped, or if I imported it incorrectly
Code; index.js (main file)
var admin = require("firebase-admin");
var serviceAccount = require("./firebase/key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
var db = admin.firestore()
Code; validate.js (import)
var db = require('../../../index')
If you just imported the Admin SDK, the syntax has changed to be modular. Check the latest documentation on initializing Firestore which shows how to initialize Firestore with the new getFirestore
top-level function:
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
...
const db = getFirestore();