简体   繁体   中英

How to check if value exists in other collection in Firebase/Firestore

I have two collections: projects and sprints. Inside projects I have a members array with objects. And using Firebase security rules I want to check if a value exists inside one of those objects.

Project example

工程实例

Sprint example

冲刺示例

What I want to do is, when a user updates or creates a sprint, I want to check if the logged in user (request.auth.uid) matches with one of the userIds inside of the members array of the project. But I just can't figure out how to accomplish this.

This is what I currently have, which does not work obviously.

match /sprints/{document} {
    allow read: if request.auth.uid != null
    allow update, create: if get(/databases/$(database)/documents/projects/$(resource.data.project)).data.members == request.auth.uid
}

Does someone maybe know how to do this?

Thanks in advance

You wont be able to do that with this data structure: you would need to loop through the members and you cant do that in the rules.

But what would work is to use a map instead of an array with the uid as the key.

{ 
  archived: false
  id: "asdasd"
  members: {
    firstuid: { name: "Bob", role: "Prog"}
    seconduid: { name: "Alice", role": "PM"}
  }
}

Then the following rules would do the job:

match /sprints/{document} {
    allow read: if request.auth.uid != null
    allow update, create: if get(/databases/$(database)/documents/projects/$(resource.data.project)).data.members.get(request.auth.uid, null) != null 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM