Home > database >  Cloud Functions Firestore import doesn't work
Cloud Functions Firestore import doesn't work

Time:02-04

I'm trying to use Firestore from Cloud functions package like this:

export async function deleteDocRecursively(docRef) {
    await Firestore.recursiveDelete(docRef);
}

This works:

const {Firestore} = require('@google-cloud/firestore');

This doesn't work:

import {Firestore} from '@google-cloud/firestore';

I get this error:

TS2339: Property 'recursiveDelete' does not exist on type 'typeof Firestore'.

https://www.npmjs.com/package/@google-cloud/firestore

CodePudding user response:

You are using client firestore inside firebase functions instead you should use the one which comes with admin one.

As you are using client firestore

const {Firestore} = require('@google-cloud/firestore');

Above one is working and

import {Firestore} from '@google-cloud/firestore';

Is being failed and getting below error

Property 'recursiveDelete' does not exist on type 'typeof Firestore'.

To get it work you have to use the admin one as this will run on the server.

As Firebase client SDK is meant to run in a client-side environment hence we use Client SDK using firebaseConfiguration listed in the firebase console.

While in firebase functions by initializing the Firebase Admin SDK with admin.initializeApp(), we can perform actions on behalf of your users and take advantage of the security and management features provided by Firebase.

When using firebase function it is advisable to use admin services as stated in the docs
If you have configured your firebase functions with typescript then follow this:

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();

const firestore = admin.firestore();

exports.deleteDocRecursively = functions.https.onRequest(async (req, res) => {
  // Delete Recursively
  const documentRef = firestore.collection("users").doc("<doc_id of doc to be deleted>");
  await firestore.recursiveDelete(documentRef);
});

If you have configured your firebase functions with javascript follow this:

const =  functions require("firebase-functions");
const = admin require("firebase-admin");
admin.initializeApp();
const firestore = admin.firestore();

exports.deleteDocRecursively = functions.https.onRequest(async (req, res) => {
  // Delete Recursively
  const documentRef = firestore.collection("users").doc("<doc_id of doc to be deleted>");
  await firestore.recursiveDelete(documentRef);
});

For more information go through these links: Thread using recursiveDelete

CodePudding user response:

As mentioned in the documentation, recursiveDelete is a method on an instance of Firestore class and not a static method on the class itself. Try:

import { Firestore } from '@google-cloud/firestore';
// const { Firestore } = require('@google-cloud/firestore');

const db = new Firestore();  


export async function deleteDocRecursively(docRef) {
    await db.recursiveDelete(docRef); // <-- not 'Firestore'
}
  • Related