【问题标题】:Firestore query array/rules not working resources.viewersFirestore 查询数组/规则不起作用 resources.viewers
【发布时间】:2019-10-02 13:05:28
【问题描述】:

我正在尝试根据此博客 https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html 从数组中获取“读取”规则

  var db = firebase.firestore();
  db.collection("_users").where("viewers", "array-contains", myUID)
    .get()
    .then((querySnapshot) => {
      querySnapshot.forEach((doc) => {
        console.log(doc.id, ' => ', doc.data());
      });
    });

在 AngularFirestore 中相同

this.itemsCollection_users = this._afDB.collection("_users", ref => ref.where("viewers", "array-contains", myUID)))

The DB 
/_users/EGsht477klOKW0YeYsryo0j8AkY2/sometext = "hello",
/_users/EGsht477klOKW0YeYsryo0j8AkY2/viewers [
  0: hKeDFsC6wOQZGz0rP7SnJ7Vo5O73,
  1: Y8xbH2avmCWmm1EBK7wappM2qE03
]

数据库规则 服务云.firestore {

  match /databases/{database}/documents {   
    match /_users/{userId} {

        allow read: if request.auth.uid in resource.viewers;    

    }
}
}

所以用户 EGsht477klOKW0YeYsryo0j8AkY2 应该可以从“查看器”中列出的任何用户访问(读取)

我得到的警告是……

ERROR FirebaseError: Missing or insufficient permissions.

【问题讨论】:

    标签: javascript angular google-cloud-firestore firebase-security


    【解决方案1】:

    安全规则中的resource 变量指向Resource object。要获取其数据,您需要使用resource.data,因此:

     allow read: if request.auth.uid in resource.data.viewers; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 2019-12-14
      • 2019-01-31
      • 1970-01-01
      • 2020-07-09
      • 2018-03-19
      • 2021-05-12
      • 2021-11-22
      相关资源
      最近更新 更多