Deleting Firestore documents with firebase-tools is not working

Viewed 1079

I am calling a cloud function from iOS to delete documents in Firestore. The function gets called and executes but the actual deletion never happens. Any idea what I'm doing wrong?

Cloud Function:

const functions = require('firebase-functions');
const firebase_tools = require('firebase-tools');

exports.recursiveDelete = functions
  .runWith({
    timeoutSeconds: 540,
    memory: '2GB'
  })
  .https.onCall((data, context) => {
    const path = data.path;
    console.log(
      `User ${context.auth.uid} has requested to delete path ${path}`
    );

    return firebase_tools.firestore
      .delete(path, {
        project: process.env.GOOGLE_CLOUD_PROJECT,
        recursive: true,
        yes: true,
        token: functions.config().fb.token
      })
      .then(() => {
        return {
          path: path
        };
      });
  });

iOS

let deleteFunction = functions.httpsCallable("recursiveDelete")
        deleteFunction.call(["path": docPath]) { (result, error) in
            if let error = error as NSError? {
                if error.domain == FunctionsErrorDomain {
                    let code = FunctionsErrorCode(rawValue: error.code)
                    let message = error.localizedDescription
                    let details = error.userInfo[FunctionsErrorDetailsKey]
                    print("\(code): \(message)\n\nDetails: \(details)")
                }
            } else {
                print("Deletion was successful")
            }
        }

Here is the console output from Xcode:

Optional(__C.FIRFunctionsErrorCode): INTERNAL

Details: nil

Here is the log from the execution: enter image description here

1 Answers

Try to check version of NodeJS. In NodeJS 10 and up, lot of environment variables were removed, including process.env.GOOGLE_CLOUD_PROJECT, which now returns undefined.

Instead of process.env.GOOGLE_CLOUD_PROJECT, you have several options:

Hardcode your project ID as a string:

.delete(path, {
        project: "myprojectid-12345",
        recursive: true,
        yes: true,
        token: functions.config().fb.token
      })

You can also pick up your project ID from firebase config:

myProjectID = JSON.parse(process.env.FIREBASE_CONFIG).projectId

Please note, as stated here:

FIREBASE_CONFIG is always populated by the Firebase CLI when deploying, but will not be present when deploying via gcloud or other means.

Or based on this documentation setup your own environment variable when deploying your function.

Related