Javascript 使用firebase函数删除Firestore集合
我正在尝试设置firebase函数,在删除文档时删除文档的所有子集合。通过阅读文档,我得到以下代码:Javascript 使用firebase函数删除Firestore集合,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我正在尝试设置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 // // http
// 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()都应该返回一个值或抛出(promise/always return)
(参数)解析:任意
以下是查看错误位置的屏幕截图:
谢谢你的帮助
更新:我改变了一些事情(添加了一个承诺):
//Firebase SDK的云函数用于创建云函数和设置触发器。
const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);
////创建并部署您的第一个云功能
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
//exports.helloWorld=functions.https.onRequest((请求,响应)=>{
//回复。发送(“来自Firebase的你好!”);
// });
exports.deleteCollectionFunction=functions.firestore
.document('exampleCollection/{exampleID}')
.onDelete((快照、上下文)=>{
//在删除之前获取表示文档的对象
//例如,{'name':'Marie','age':66}
const deletedValue=snap.data();
const exampleID=context.params.exampleID;
常量批次大小=500;
const database=admin.firestore();
const commentsRef=database.collection('exampleCollection').doc(exampleID).collection(“comments”);
commentsRef.doc('main').delete();
const exampleRef=database.collection('examplecolection').doc(exampleID).collection(“exampleSubCollection”);
const deleteExamples=deleteCollection(数据库、exampleRef、批大小)
返回承诺。全部([deleteExamples]);
});
/**
*以批次大小删除集合。请注意,这是正确的
*不递归删除集合中文档的子集合
*/
函数deleteCollection(db、collectionRef、batchSize){
var query=collectionRef.orderBy(“名称”).limit(batchSize)
返回新承诺(功能(解决、拒绝){
deleteQueryBatch(数据库、查询、批大小、解析、拒绝)
})
}
函数deleteQueryBatch(数据库、查询、批大小、解析、拒绝){
query.get()
。然后((快照)=>{
//当没有文件时,我们就完蛋了
如果(snapshot.size==0){
返回0
}
//批量删除文档
var batch=db.batch()
snapshot.docs.forEach(函数(doc){
批处理删除(doc.ref)
})
返回batch.commit().then(函数(){
返回快照大小
})
}).then(函数(numDeleted){
如果(numDeleted改用admin.initializeApp();
您没有正确构造函数,您应该在一个函数中处理所有这些任务。因此,如果您决定调用函数,请确保函数返回承诺,因此在调用函数时应使用return deleteCollection()@d.mares这段代码的大部分来自,一部分来自delete collections文档,另一部分来自Firestore triggers文档。我对云函数没有任何经验,只是放了一些片段together@d.mares我更新了我的代码,你能看一下吗?谢谢saddconst exampleID=context.params.exampleID;
Ok t他工作了,现在我得到了这个错误:ReferenceError:exampleID未定义add const exampleID=context.params.exampleID
;在firebase函数控制台中函数执行79毫秒后,完成状态为:“error”
,我把const exampleID=context.params.exampleID;放在const deletedValue=snap.data()下面;您收到另一个错误。请使用错误详细信息更新问题。OK,我使用新的错误和代码再次更新了问题(顺便说一句,谢谢您的帮助)
// 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)
}