Firebase Functions - FirebaseError: Missing required options (force) while running in non-interactive mode

Firebase Functions - FirebaseError: Missing required options (force) while running in non-interactive mode

我有一个 Firebase 函数,当用户的帐户被删除时,它会删除 Firestore 数据库中的用户集合。

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

exports.deleteUser = functions.auth.user().onDelete((user) => {
  return firebase_tools.firestore
      .delete(`users/${user.uid}`, {
        project: process.env.GCLOUD_PROJECT,
        token: functions.config().fb.token,
        recursive: true,
        yes: true
      }).catch((error) => {
        console.log(error);
        throw new functions.https.HttpsError(
            "unknown",
            "Error deleting user's data"
        );
      });
});

每当删除用户并执行函数时,我都会在函数日志中收到以下错误。

FirebaseError: Missing required options (force) while running in non-interactive mode
    at prompt (/workspace/node_modules/firebase-tools/lib/prompt.js:16:15)
    at promptOnce (/workspace/node_modules/firebase-tools/lib/prompt.js:29:11)
    at Command.actionFn (/workspace/node_modules/firebase-tools/lib/commands/firestore-delete.js:69:51)
    at Object.delete (/workspace/node_modules/firebase-tools/lib/command.js:190:25)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

我能找到的与此相关的唯一信息是关于 Firebase 的 deploying/deleting 函数,我找不到太多关于 firebase 工具的文档。

我已经重现了您遇到的错误。

这个错误发生在最近的"firebase-tools": "^10.1.3"

基于 Delete data with a Callable Cloud Function, the documentation have sample code that still uses "firebase-tools": "9.18.0"。 您可以通过修改 package.json 来降级 firebase-tools。例如。以下:

  "dependencies": {
    "firebase": "^9.6.5",
    "firebase-admin": "^9.12.0",
    "firebase-functions": "^3.16.0",
    "firebase-tools": "9.18.0"
  }

降级后,我可以成功删除指定文档。

您也可以使用@Renaud Tarnec 通过 Admin SDK 回答的内容。 例如。以下:

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

db = admin.firestore();

exports.deleteUser = functions.auth.user().onDelete((user) => {
    db.collection("users").doc(user.uid).delete()
      .then(function(user) {
        console.log("Successfully Deleted User:", user.uid)
        })
      .catch((error) => {
        console.log(error);
        throw new functions.https.HttpsError(
            "unknown",
            "Error deleting user's data"
        );
      });
});

添加强制:传递给 firebase-tools 的 JSON 为真。为我工作的版本 10.1.4

{
        project: process.env.GCLOUD_PROJECT,
        token: functions.config().fb.token,
        recursive: true,
        yes: true,
        force: true // add this
}