【问题标题】:AngularFire2: ERROR Error: Missing or insufficient permissionsAngularFire2:ERROR 错误:权限缺失或不足
【发布时间】:2018-08-07 13:31:08
【问题描述】:

不知道为什么我在访问 AngularFirestoreCollection 时遇到此错误:

ERROR 错误:权限缺失或不足。

this.categories = afs.collection('categories').doc(this.afAuth.auth.currentUser.uid).collection<Category>('categories');

Firestore 数据库规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /categories/{userId}{
      allow read, write: if request.auth.uid == userId;
    }
  }
}

我在这里错过了什么?

【问题讨论】:

  • 从您的代码看来,您正试图访问/categories/{userId}/categories,但您的规则仅适用于/categories/{userId}。尝试将其更改为match /categories/{category=**} {

标签: firebase google-cloud-firestore angularfire2 firebase-security


【解决方案1】:

您已在应用程序中配置了深层路径:

Categories --> User --> Categories

您的规则只包含父文件夹:

/categories/{userId}

你必须改为:

/categories/{userId}/categories/{category}

因此您可以访问 UserId 以获取匹配条件,以及当前路径的效果。

何时使用通配符以及何时使用特定或嵌套匹配条件有很多原因,请查看 main docs 并选择最适合您的实现。

【讨论】:

  • 我尝试使用通配符 ( /categories/{userId=**} ) 但得到了同样的错误。但是按照建议使用完整路径是可行的。我是否正确使用通配符?
  • 我用来存储用户类别的结构是正确的还是更有效的方式?
  • 我不确定您的应用程序的目标是什么,但是如果您需要为每个用户管理额外的类别集合,那似乎还可以。
【解决方案2】:

有同样的问题。

在拨打电话之前检查您是否已经登录:

isSignedIn() {
  this.auth.onAuthStateChanged((user) => {
    if (user) {
    // User is signed in.
    console.log("User is signed in.");

    } else {
      console.log("No user is signed in.");
    }
});

【讨论】:

    猜你喜欢
    • 2018-08-24
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2018-09-12
    • 2020-10-14
    • 2018-03-17
    相关资源
    最近更新 更多