通过在Cloud Functions中创建的函数删除集合
当用户被删除时,我需要删除包含多个文档的整个集合,因此我决定创建一个函数,以便在用户被删除时触发。
以下是代码。
Cloud Functions (index.js):
exports.onUserDeleted = functions.auth.user().onDelete((user) => {
deleteCollection(admin.firestore(), user.uid, 15);
});
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);
}
尝试执行firebase deploy命令--only functions时,出现以下错误:
65:11 警告 箭头函数应该没有返回值 consistent-return
65:18 警告 避免嵌套Promise promise/no-nesting
69:11 错误 每个 then() 应该返回一个值或抛出异常 promise/always-return
✖ 3 个问题(1 个错误,2 个警告)
我做错了什么?
当用户被删除时,如何删除整个集合?
如果您能发布上述代码并提供我应该应用的逻辑,我将非常感激。