javascript - 使用 firebase 功能删除 Firestore 集合
问题描述
我正在尝试设置一个 firebase 函数,该函数在删除文档时删除文档的所有子集合。通过阅读文档,我得到了这段代码:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
exports.DeleteColletionFunction = functions.firestore
.document('exampleCollection/{exampleID}')
.onDelete((snap, context) => {
// Get an object representing the document prior to deletion
// e.g. {'name': 'Marie', 'age': 66}
const deletedValue = snap.data();
deleteCollection()
});
function deleteCollection(db, collectionPath, batchSize) {
var collectionRef = db.collection(collectionPath);
var query = collectionRef.orderBy('__name__').limit(batchSize);
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, batchSize, resolve, reject);
});
}
function deleteQueryBatch(db, query, batchSize, resolve, reject) {
query.get()
.then((snapshot) => {
// When there are no documents left, we are done
if (snapshot.size == 0) {
return 0;
}
// Delete documents in a batch
var batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
return batch.commit().then(() => {
return snapshot.size;
});
}).then((numDeleted) => {
if (numDeleted === 0) {
resolve();
return;
}
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, batchSize, resolve, reject);
});
})
.catch(reject);
}
我以前从未使用过云功能,因此不确定下一步应该做什么。我看到为了使用删除集合功能,必须传递一个数据库、collectionPath 和 batchSize。在这种情况下传递的正确值是什么?
我应该使用这行代码来获取 firestore 数据库吗?
const database = admin.firestore();
从文档中复制此功能时,我也遇到了一些错误:
预期 '===' 而是看到 '=='
[eslint] 避免嵌套承诺。(承诺/无嵌套)(参数)快照:任何
[eslint] 每个 then() 都应该返回一个值或抛出 (promise/always-return) (参数) resolve: any
谢谢您的帮助!
更新:
我改变了一些东西(添加一个承诺):
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
exports.DeleteColletionFunction = functions.firestore
.document('exampleCollection/{exampleID}')
.onDelete((snap, context) => {
// Get an object representing the document prior to deletion
// e.g. {'name': 'Marie', 'age': 66}
const deletedValue = snap.data();
const exampleID = context.params.exampleID;
const BATCH_SIZE = 500;
const database = admin.firestore();
const commentsRef = database.collection('exampleCollection').doc(exampleID).collection("comments");
commentsRef.doc('main').delete();
const exampleRef = database.collection('exampleCollection').doc(exampleID).collection("exampleSubCollection");
const deleteExamples = deleteCollection(database, exampleRef, BATCH_SIZE)
return Promise.all([deleteExamples]);
});
/**
* Delete a collection, in batches of batchSize. Note that this does
* not recursively delete subcollections of documents in the collection
*/
function deleteCollection (db, collectionRef, batchSize) {
var query = collectionRef.orderBy('__name__').limit(batchSize)
return new Promise(function (resolve, reject) {
deleteQueryBatch(db, query, batchSize, resolve, reject)
})
}
function deleteQueryBatch (db, query, batchSize, resolve, reject) {
query.get()
.then((snapshot) => {
// When there are no documents left, we are done
if (snapshot.size === 0) {
return 0
}
// Delete documents in a batch
var batch = db.batch()
snapshot.docs.forEach(function (doc) {
batch.delete(doc.ref)
})
return batch.commit().then(function () {
return snapshot.size
})
}).then(function (numDeleted) {
if (numDeleted <= batchSize) {
resolve()
return
}
else {
// Recurse on the next process tick, to avoid
// exploding the stack.
return process.nextTick(function () {
deleteQueryBatch(db, query, batchSize, resolve, reject)
})
}
})
.catch(reject)
}
现在我在 firebase 控制台中遇到错误:
ReferenceError:exampleID 未在对象的exports.DeleteColletionFunction.functions.firestore.document.onDelete (/user_code/index.js:26:66) 中定义。(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) 在下一个(本机)在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) 在 /var/ tmp/worker/worker.js:728:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)
谢谢你的帮助!
解决方案
改为使用admin.initializeApp();
推荐阅读
- business-process-management - 如何使用实例 ID 从流程实例中提取数据
- javascript - 修改参数时如何覆盖函数?
- python - django 和 boto3 的 AWS S3 图像上传问题
- cloud - 如何使用 gsutil 从文件名中删除 .jpg 已在存储桶中超过 20000 个文件
- html - 如何使用 ngfor 读取的元素作为函数参数
- java - 在 Android 6.0 上切换飞行模式
- javascript - 如何使用触发器 OnEdit () 将一个单元格 (A1) 复制到另一个单元格 (A2)
- python - 如何在 groupby 函数上应用 nsmallest
- javascript - ng-switch 不工作,即使 isactive 为假
- c# - 在使用 Newtonsoft 之后,ajax 调用在 asp.net 中返回 HTML 响应而不是 Json