Firebase安全规则为Firestore .where()快照引发错误

时间:2019-08-26 18:32:45

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

获取错误:

  

onSnapshot中未捕获的错误:FirebaseError:权限丢失或不足。

这是被调用的函数:

let unsubscribeUserThings
export function getUserThings(user, state) {
    unsubscribeUserThings = firebaseDB.collection("things").where("user", "==", user)
        .onSnapshot(function(querySnapshot) {
            var things = {}
            querySnapshot.forEach(function(doc) {
                things[doc.id] = doc.data()
            })

            state.setState({
                things: things
            })
        })

}
export function stopListeningToUserThings() {
    // Stop listening to changes
    unsubscribeUserThings()
}

Firebase安全规则为:

service cloud.firestore {
  match /databases/{database}/documents {

    // FUNCTIONS

      // True if the signed in user is the thing's user
      function isThingUser() {
        return request.auth != null && request.auth.uid != null && resource.data.user == request.auth.uid
      }

      // True if the signed in user is the thing's owner
      function isThingOwner() {
        return request.auth != null && request.auth.uid != null && resource.data.owner == request.auth.uid
      }

    // RULES

      // Things
      match /things/{thingID} {
        allow create: if true
        allow read, update: if isThingUser() || isThingOwner()
      }
  }
}

奇怪的是,它仍然可以正确显示所有内容,只是引发了此错误。为什么会这样呢?我会忽略吗?

1 个答案:

答案 0 :(得分:0)

使用Firebase规则,一切都很好,而且很好–最终,它被另一个onSnapshot调用拉动,以监听“事物”文档的子集合,即子集合“部件”。添加了一个嵌套的match语句,以授予其读取/ things / {thingID} / parts / {partID}的权限,并且一切现在都在工作!

service cloud.firestore {
  match /databases/{database}/documents {

    // FUNCTIONS

      // True if the signed in user is the thing's user
      function isThingUser() {
        return request.auth != null && request.auth.uid != null && resource.data.user == request.auth.uid
      }

      // True if the signed in user is the thing's owner
      function isThingOwner() {
        return request.auth != null && request.auth.uid != null && resource.data.owner == request.auth.uid
      }

  // RULES

    // Things
    match /things/{thingID} {
      allow create: if true
      allow read, update: if isThingUser() || isThingOwner()

      match /parts/{partID} {
        allow read, write: if true
      }
    }
  }
}