我正在尝试设置一个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);
}
我以前从未使用过云功能,因此不确定下一步该做什么。我看到,为了使用delete Collection函数,必须传递数据库,collectionPath和batchSize。在这种情况下要传递的正确值是什么?
我应该使用此行代码来获取Firestore数据库吗?
const database = admin.firestore();
从文档复制此功能时,我也遇到一些错误:
预期为'===',而是看到了'=='
[eslint]避免嵌套承诺。 (承诺/无嵌套) (参数)快照:任意
[eslint]每个then()应该返回一个值或抛出(承诺/总是返回) (参数)解决:任何
谢谢您的帮助!
更新:
我更改了一些内容(添加了承诺):
// 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 在export.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 在process._tickDomainCallback(internal / process / next_tick.js:135:7)
感谢您的帮助!
答案 0 :(得分:1)
改为使用admin.initializeApp();