I want to write a rule that will don't allow add same document second time

早过忘川 提交于 2020-01-25 09:21:25

问题


rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{usersID} {
    allow read;
    allow write: if request.auth.uid != null;
        match /desksCollection/{desksCollectionID} {
        allow read;
        allow write: if resource.data.DeskName != request.resource.data.DeskName;
      }
    }
  }
}

I want to write a rule that will don't allow add same document second time. What is incorrect here ?


回答1:


What you are trying to do is not possible with security rules. This would require that you be able to perform a query on desksCollection for documents that match the criteria of having a field with a certain value. However, performing queries is not possible in security rules. You can fetch a document by its ID, but that won't help you here, because you don't know which document to fetch.

If you require that deskName be unique, then consider using that value as the ID of the document. Then you can write a rule to prevent updates if the document already exists.



来源:https://stackoverflow.com/questions/56865407/i-want-to-write-a-rule-that-will-dont-allow-add-same-document-second-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!