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

自闭症网瘾萝莉.ら 提交于 2021-02-10 18:27:45

问题


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)

回答1:


As far as I can see from reading the code of the delete function, the path you pass in has to be a single string value of the collection or document to delete. The function will then delete all data under that collection or document.




回答2:


the solution

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


来源:https://stackoverflow.com/questions/59410008/typeerror-this-path-replace-is-not-a-function-at-new-firestoredelete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!