我正在尝试使用可调用的 firebase 函数删除集合。
我从 here 获取了一个示例代码.
示例代码有一个“firebase_tools”元素。
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
这显然不是 firebase-function
的一部分我在功能日志中收到以下错误:
Unhandled error ReferenceError: firebase_tools is not defined
该文件还提到:You can import any function of the Firebase CLI into your own Node.js application using the firebase-tools package.
但我不知道该怎么做。
编辑:在使用 firebase 函数时,学习 nodejs 基础知识将对您有很大帮助。
最佳答案
如果您查看您引用的同一文档,您会发现示例代码的链接:https://github.com/firebase/snippets-node/tree/master/firestore/solution-deletes
然后,如果您查看 Cloud Function 代码 ( index.js
),您将看到此示例的开头如下:
const admin = require('firebase-admin');
const firebase_tools = require('firebase-tools');
const functions = require('firebase-functions');
admin.initializeApp();
所以你需要:
firebase-tools
包正确安装。通常情况下应该是这样,因为您很可能已经在使用 Firebase CLI(请参阅 https://github.com/firebase/firebase-tools)。如果没有,请使用 npm i firebase-tools
安装它. const firebase_tools = require('firebase-tools');
将其导入您的云函数中。如示例所示。 然后您就可以调用
firebase_tools.firestore.delete()
.
关于尝试从云功能中删除 firestore 集合时未找到 firebase_tools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916337/