'access firestore from firebase-functions using web version 9 interface (modular)

I have to access a firestore database within firebase functions. I use the following code to get a firestore instance:

import { initializeApp } from "firebase-admin/app"
initializeApp();

import { getFirestore } from 'firebase-admin/firestore'
const db = getFirestore();

This instance can be used to access the database

db.collection('test').add( { anything: 123 } )
db.collection('test').doc('test123').set( { afield: 1234 } )
db.collection('test').doc('test123').get()

According to https://firebase.google.com/docs/firestore/query-data/queries#web-version-8 this is "Web version 8 (namespaced)" interface for accessing firestore databases.

How can I use the "Web version 9 (modular)" interface? The version 9 example from the same page:

import { collection, doc, setDoc } from "firebase/firestore"; 
const citiesRef = collection(db, "cities");

results in the following error:

FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

Is there a way to query firestore with admin privileges using modular interface?

Thank You!



Solution 1:[1]

The Admin SDK does not use functional syntax totally yet. The Web version 8/9 are client SDK. Refer to NodeJS tab of the documentation when using Firebase Admin SDK.

Also you are importing collection() from Firebase client SDK but it seems the db is initialized using getFirestore() from Firebase SDK that might not work properly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharmaraj