firebase - 当用户没有足够的权限写入数据库时,如何捕获 firestore 权限被拒绝异常?
问题描述
当我尝试使用没有写入权限的用户帐户写入数据库时,我按预期得到了这个异常
发生异常。PlatformException (PlatformException(firebase_firestore, com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or enough permissions., {code: permission-denied, message: 调用者没有执行指定操作的权限。}))
但我无法抓住它。
调用此方法会导致异常
Future<void> addRecord(Product product) async {
try {
await FirebaseFirestore.instance
.collection("products")
.doc()
.set(product.toMap())
.catchError((err) {
print("error is" + err.toString());
});
} on PlatformException catch (e) {
print("eee" + e.toString());
}
}
我相信这个异常是在控制台中捕获的,我收到了这条消息:
eee[cloud_firestore/permission-denied] 调用者没有执行指定操作的权限。
但是控制台中还有一条消息
W/Firestore(12930): (21.4.3) [Firestore]: 在 products/SPeUO9260uCoIfpDsSag 写入失败:状态{code=PERMISSION_DENIED,描述=缺少或权限不足。,原因=null}
解决方案
推荐阅读
- c# - DataGridView:“System.DBNull”类型的对象无法转换为“System.Decimal”类型
- php - 为什么 mysqli_query 的结果返回一个其字段设置为 null 的对象?
- php - 登录后无法访问 wordpress 网站上的 wp-admin 页面
- azure-pipelines-yaml - Azure Pipelines YAML Web 编辑器在保存前自动验证
- javascript - 发生 404 时处理 Promise 未解决
- blockchain - 如何使用您自己的节点跟踪特定钱包的交易?
- python - 使用 getattr 调用函数时返回类 NoneType
- flutter - Flutter 中 XFile 类型的图像文件质量是否低于 File 类型的图像?
- javascript - 在两个值之间更改范围输入颜色
- python - 如何在 Python 中使用 Dom 库