[英]How to detach a listener for a Firestore collection made with AngularFire?
[英]How to do remove an entire document collection in angularfire firestore
我刚刚更改了我的代码,如下所示,它正在工作。 在 google cloud-firestore 中可以批量删除。
async deleteMembersOfGroup(clubId: string, groupId: string) {
//path of the collection
const path = "club/" + clubId + "/group/" + groupId + "/group-members";
//query snapshot
const qry: firebase.firestore.QuerySnapshot = await this.afs.collection(path).ref.get();
const batch = this.afs.firestore.batch();
//looping through docs in the collection to delete docs as a bulk operation
qry.forEach(doc => {
console.log('deleting....', doc.id);
batch.delete(doc.ref);
});
//finally commit
batch.commit().then(res => console.log('committed batch.'))
.catch(err => console.error('error committing batch.', err));
}
文档说:
不建议从 Web 客户端删除 collections。
因此省略给出示例(尽管提供了 Node.js 示例)。
因此,您可以调整 Node.js 代码以在 JavaScript 中工作(尽管 Google 不建议这样做),或者您可以使用云 function 解决方案(如果您使用的是 Google 提供的云功能)。
使用 Callable Cloud 删除集合 Function
这是直接来自文档的云 function 代码:
云 FUNCTION 代码
/**
* Initiate a recursive delete of documents at a given path.
*
* The calling user must be authenticated and have the custom "admin" attribute
* set to true on the auth token.
*
* This delete is NOT an atomic operation and it's possible
* that it may fail after only deleting some documents.
*
* @param {string} data.path the document or collection path to delete.
*/
exports.recursiveDelete = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall(async (data, context) => {
// Only allow admin users to execute this function.
if (!(context.auth && context.auth.token && context.auth.token.admin)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
// Run a recursive delete on the given document or collection path.
// The 'token' must be set in the functions config, and can be generated
// at the command line by running 'firebase login:ci'.
await firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
force: true,
token: functions.config().fb.token
});
return {
path: path
};
});
客户端代码
/**
* Call the 'recursiveDelete' callable function with a path to initiate
* a server-side delete.
*/
function deleteAtPath(path) {
var deleteFn = firebase.functions().httpsCallable('recursiveDelete');
deleteFn({ path: path })
.then(function(result) {
logMessage('Delete success: ' + JSON.stringify(result));
})
.catch(function(err) {
logMessage('Delete failed, see console,');
console.warn(err);
});
}
一如既往,为什么 GCP 这么难!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.