TypeError: this.path.replace is not a function at new FirestoreDelete

Trying to remove the users`s data calling a function from app.

'use strict';
const functions = require('firebase-functions');
const firebase_tools = require('firebase-tools');
const admin = require('firebase-admin');
const serviceAccount = require('./myapp.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://myapp.firebaseio.com"
});
let db = admin.firestore(); 

exports.mintAdminToken = functions.https.onCall((data, context) => {
  const uid = data.uid;

  return admin
    .auth()
    .createCustomToken(uid, { admin: true })
    .then(function(token) {
      return { token: token };
    });
});

exports.recursiveDelete = functions
  .runWith({
    timeoutSeconds: 540,
    memory: '1GB'
  })
  .https.onCall((data, context) => {

    if (!(context.auth && context.auth.token )) {
      throw new functions.https.HttpsError(
        'permission-denied',
        'Must be an administrative user to initiate delete.'
      );
    }
    let path = data.path;
    console.log(
      `User ${context.auth.uid} has requested to delete path ${path}`
    );

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

and I pass the path like:

    Map<String, Object> data = new HashMap<>();
    data.put("path", "./users/rnAjpK4LLSMMlENZqe4l3F2");

result:

Function execution took 540003 ms, finished with status: 'timeout'

probably the problem is in path. if I change for this line:

let path = admin.firestore().doc('users/' + context.auth.uid);

Im getting an error

Unhandled error TypeError: this.path.replace is not a function at new FirestoreDelete

is the problem with “path”? what will be the right path to delete then?

I use this example https://github.com/firebase/snippets-node/tree/master/firestore/solution-deletes but here is nothing about it

UPDATED:

with String path = “./users/rnAjpK4LLSMMlENAgrZqe4l3F2”; or String data = “./users/rnAjpK4LLSMMlENAgrZqe4l3F2”;

an error

Unhandled error { FirebaseError: Must specify a path. at Object.reject (/srv/node_modules/firebase-tools/lib/utils.js:82:27)

Answer

the solution

   const id = context.auth.uid;
   const path = `users/${id}`;

Leave a Reply

Your email address will not be published. Required fields are marked *