4

Is it possible to create rules denied if a price is already registered in the database? I tried to validate using exist data but firebase blocks all of them when there is one data

database.ref('data/penawaran/enter-name').push({
    name: 'Udin',
    price:'300000',
    member_id:9,
    product_id:8,
    created_at: '2020-10-13 23:00:00'
}, function(error) {
    if (error) {
       alert('error');
    } else {
       
    }
});

^^^ i want get failed cause price 300000 already exist in database

database.ref('data/penawaran/enter-name').push({
    name: 'Udin',
    price:'900000',
    member_id:9,
    product_id:8,
    created_at: '2020-10-13 23:00:00'
}, function(error) {
    if (error) {
       alert('error');
    } else {
       
    }
});

i want get success cause price 900000 not exist in database

enter image description here

enter image description here

Frans Suny
  • 63
  • 7
  • Since the rules by themselves don't do anything, you should edit the question to also show the app code that doesn't work the way you exepct. – Doug Stevenson Oct 11 '20 at 21:49
  • hello im already edit the question and add the app code, thank you :) – Frans Suny Oct 11 '20 at 21:59
  • We need to be able to see the [complete, minimal code](https://stackoverflow.com/help/minimal-reproducible-example). We can't see how you build the reference, since we don't know what `roomName` is, or any of the other variables. You should hard code all variables with actual values. – Doug Stevenson Oct 11 '20 at 22:11
  • hello thank you, im already update my example code – Frans Suny Oct 11 '20 at 22:22
  • 1
    There is no way to ensure a unique **value** in the database through security rules. I you want something to be unique, make it the key - since keys are by definition unique within their context. – Frank van Puffelen Oct 12 '20 at 05:59
  • thank you for information.. im trying change price to key ,, :) – Frans Suny Oct 13 '20 at 05:03

0 Answers0