0

Using post method, data is posted like below. Every node has some unique ID. So how can I prevent duplicate data from posting into this JSON data model using rules in firebase?

REST POST URL: https://*****.firebaseio.com/user_details.json

{

"-Ktzg49_ArPtNZ9SDpIw": {
    "email": "amee11315@gmail.com",
    "name": "Abir Hossain Amee",
    "profile_photo": "https:\/\/graph.facebook.com\/1928067980740489\/picture?type=large"
  },
  "-KtzpFMjjcvN7Mooh-4I": {
    "email": "arshohag7@gmail.com",
    "name": "Ashiqur Rahman",
    "profile_photo": "https:\/\/graph.facebook.com\/1570862736318692\/picture?type=large"
  },
  "-KtzpbLGDRBo9dH3uZSZ": {
    "email": "arshohag7@gmail.com",
    "name": "Ashiqur Rahman",
    "profile_photo": "https:\/\/graph.facebook.com\/1570862736318692\/picture?type=large"
  },
  "-Ktzq3HgVp9ykErVXg_8": {
    "email": "arshohag7@gmail.com",
    "name": "Ashiqur Rahman",
    "profile_photo": "https:\/\/graph.facebook.com\/1570862736318692\/picture?type=large"
  }

}

I tried these rules

{
  "rules": {
    ".read": "true",
    ".write": "true",
        "user_details" : {
            "$email" : {
                ".write": "!data.exists()"
            }
        }      
  }
}

But can't stop duplication or achieve normalization.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ashiqur Rahman
  • 425
  • 7
  • 21
  • There is no built-in operation in the Firebase Database to prevent multiple nodes from having the same value. On the other hands, the keys in a collection are by definition unique. So the solution is to use the values you want to be unique as keys. This has been covered quite a few times before, so I'll refer you to https://stackoverflow.com/q/35243492 (and the links from my comment there) – Frank van Puffelen Sep 24 '17 at 05:18
  • Calling `POST` generates the push ID. These solution will require that you write to a known path, so by using use the HTTP `PUT` verb at a known path. – Frank van Puffelen Sep 24 '17 at 05:21

0 Answers0