0

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

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
vovahost
  • 34,185
  • 17
  • 113
  • 116
  • If my answer below does not work, can you please confirm which version of `@google-cloud/firestore` are you using? – Dharmaraj Feb 03 '23 at 11:19

2 Answers2

1

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

Rohit Kharche
  • 2,541
  • 1
  • 2
  • 13
  • The Admin SDK internally uses `@google-cloud/firestore` and its easier to use just the Firestore package when you are not using any other Firebase services. Why would using ES6 import fail with that reason? – Dharmaraj Feb 03 '23 at 11:16
  • Yeah I have got that now after taking a look at again : ) will edit the answer with cloud functions way – Rohit Kharche Feb 03 '23 at 11:17
1

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'
}
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84