此问题已在此处有答案:
How to get all of the collection ids from document on Firestore? [duplicate](1个答案)
昨天关门了。
我试图删除firebase收集文件,并与删除所有的子集合,并删除从认证也使用flutter。这里是我的数据库截图
我想删除用户文档与那些子集合(书籍和PDF)。
我的代码
void deleteUserDocument() async {
final sp = context.watch<SignInProvider>();
try {
// Get the current user's ID
final user = FirebaseAuth.instance.currentUser;
final uid = user?.uid;
if (uid != null) {
// Get a reference to the user's document in Firestore
final docRef = FirebaseFirestore.instance.collection("users").doc(uid);
// Delete all subcollections in the user's document
final collections = await FirebaseFirestore.instance.collection("users").doc(uid).listCollections();
final batch = FirebaseFirestore.instance.batch();
for (final collectionRef in collections) {
final querySnapshot = await collectionRef.get();
for (final docSnapshot in querySnapshot.docs) {
batch.delete(docSnapshot.reference);
}
batch.delete(collectionRef);
}
await batch.commit();
// Delete the user's document
await docRef.delete();
// Remove the user from Firebase Authentication
await user?.delete();
// Log the user out and navigate to the login screen
sp.userSignOut();
nextScreenReplace(context, const LoginScreen());
}
} catch (e) {
// Handle any errors that occur during the deletion process
print('Error deleting user document: $e');
// Display an error message to the user
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Failed to delete account')),
);
}
}
但在那里显示错误final collections = await FirebaseFirestore.instance.collection(“users”).doc(uid).listCollections();
错误截图
如何解决这个错误?
2条答案
按热度按时间hivapdat1#
根据删除文档Firebase文档
警告:删除单据并不删除其子集合!
然后呢
不建议客户端删除收藏。
相反,您应该删除具有Delete data with a Callable Cloud Function子集合的文档,并从客户端应用程序调用它。
Firebase Docs还提供了一个示例firebase函数来删除文档,您可以在这里检查:解决方案:使用可调用的云函数删除数据
1bqhqjot2#
看来有两个问题需要你去解决。
下面的代码解决方案解决了所有这些问题: