0

This is my database structure for the Users.

This is my database structure

As you can see in the picture, there are 2 same Identification number are registered successfully.

How can I prevent the "ICNumber"(Identification Number) to be registered more than once?

This is the rule in my RealTime Database:

This is the rule in my RealTime Database

I tried putting this piece of code but it doesn't seem to work. Is there any way I can do the prevention in my Javascript or any correct way in my Firebase Rule?

2

1 Answer 1

1

In your code, you can do the following:

// Get a reference to the database service
var database = firebase.database();
var dataRetrieved = database.ref('users').child(userId).orderByChild("ICNumber").equalTo("000326141809");
dataRetrieved.on('value', function(snapshot) {
  if(snapshot.exists()){
     // do the required
   }
});

You can use a query to check if you have an ICNumber equal to 000326141809 and then use the method exists() to check if it is already in the database

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! Your code works, it detects whether or not the value is already in the database. However, the code still runs my signup() function. Do you have any workaround for this? I've tried: break, return, and preventDefault statements, but they don't seem to work
Just write the signup inside the else and use once instead of on
Aight man understood! Thanks for the help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.