我试图在云函数中实现递归删除,但删除函数返回错误[firebase_functions/internal] FirebaseError:删除失败。错误:获取文档删除失败>= 3次…
它非常类似于这篇文章,但我尝试更新firebase工具,得到一个新的令牌,没有变化。我尝试通过添加process.env.DEBUG = true来获取调试日志,但我得到了一个类型错误,它必须是一个字符串-所以我将true更改为"true"它似乎没有给我任何日志(除非我只是不知道去哪里找它们)。
函数正在部署,虽然我得到消息:
[2022-12-08T22:01:07.693Z]找不到功能项目/onana -produce/locations/australian - southeastern 1/functions/recursiveDelete
功能如下:
/* eslint @typescript-eslint/no-var-requires: "off" */
import * as functions from "firebase-functions";
process.env.DEBUG = "true";
const firebaseTools = require("firebase-tools");
/**
* Initiate a recursive delete of documents at a given path.
*
* The calling user must be authenticated.
*
* This delete is NOT an atomic operation and it's possible
* that it may fail after only deleting some documents.
*
* @param {string} data.path the document or collection path to delete.
*/
exports.recursiveDelete = functions
.region("australia-southeast1")
.runWith({
timeoutSeconds: 540,
memory: "2GB",
})
.https.onCall(async (data, context) => {
// Only allow authorised users to execute this function.
if (!(context.auth && context.auth.token)) {
throw new functions.https.HttpsError(
"permission-denied",
"Must be an administrative user to initiate delete."
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
// Run a recursive delete on the given document or collection path.
// The 'token' must be set in the functions config, and can be generated
// at the command line by running 'firebase login:ci'.
try {
await firebaseTools.firestore
.delete(path, {
project: process.env.GCP_PROJECT,
recursive: true,
force: true,
token: process.env.TOKEN,
});
return {
path: path,
};
} catch (err) {
throw new functions.https.HttpsError("internal", String(err));
}
});
这是客户端代码(用flutter编写):
/// Call the 'recursiveDelete' callable function with a path to initiate
/// a server-side delete.
void deleteAtPath(String path) async {
print("delete: deleting at path " + path);
try {
final HttpsCallableResult deleteFnResult = await functions
.httpsCallable('recursiveDelete')
.call(<String, String>{
"path": path,
});
print(deleteFnResult.toString());
} on FirebaseFunctionsException catch (e) {
print("firebase error: " + e.toString() + " for deleting path " + path);
} catch (e) {
print("path delete error " + e.toString() + " for deleting path " + path);
}
}
deleteUserProfile(String _uid) async {
deleteAtPath(_userCollection.doc(_uid).path);
deleteAtPath(_locationsCollection.doc(_uid).path);
deleteEachProduct(productsFromUserProfileQuery(_uid));
return;
}
错误:
获取文档删除失败
这是我的包裹。json文件:
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"build:watch": "tsc --watch",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^11.3.0",
"firebase-functions": "^4.1.0",
"firebase-tools": "^11.17.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^0.2.0",
"typescript": "^4.5.4"
},
"private": true
}
同样,我可以通过命令行删除,例如firebase firestore:delete users/iALet1LOthcw0Nx6FMgP6nOa3N83
works
我已经与firebase功能战斗了好几天,所以任何帮助都会非常感激!!
解决方案:我弄清楚了如何访问日志,并看到由于某种原因项目未定义。我换回从废弃的process.env中获取项目id。GCLOUD_PROJECT代替process.env。GCP_PROJECT,它现在工作了!