0

So I am new to the Firebase database and what I like about it is that I don't have to build a whole backend for just storing some simple data. What I am trying to do is pushing data to an array that I like to recieve from firebase. Then after that I would like to check if the email that was filled in, is included in the data from the firebase database. But because it's firebase and it has multiple arrays, objects etc I don't know how to check that. So the flow is: User fills in data, Applications makes a call to the firebase db and the Application is retrieving the current data from firebase. Then the Application will check if the data that is inputed is already there, and if so, will throw an alert that the data is already in the database. If not, the data will be submitted.

Also, I am wondering if this is the right way to retrieve data from the database:

Main.js

function writeUserData() {

    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;

    firebase.database().ref('/aanmeldingen/').push({
      username: name,
      email: email,
    });

    var dbRef = firebase.database().ref().child('/aanmeldingen/');
    dbRef.on('value', snapshot => {
      const snap = snapshot.val();
      const array = [];

      array.push(snap);
      console.log(array);

      const res = array.includes(email);
      console.log(res);
      console.log(email);
    });
 }

Output in console

enter image description here

As you can see this returns multiple data. The include function will check on the submitted emailadress. This returns false even I had inputted "info@webpack.com". How can I check the right data object? It has to check all objects under "0" and return in the console if the submitted emailadress is already there.

Giesburts
  • 6,879
  • 15
  • 48
  • 85
  • It appears as if Firebase should return an object when you do a push and I would expect it to contain the IDs of your entries that you can use to reference it later (i.e. the funny strings that you see when you read your data later) to check if they are in the DB. Otherwise you would make a different query to check if a record with specific value of the "email" key exists. – mnewelski Sep 09 '17 at 07:43

2 Answers2

0

I haven't tested it yet but i hope you get the idea. Also this is not the most efficient way to do this.

function ifEmailExist(arr,email){
    var _t = 0;
    for(var x in arr){
        for(var y in arr[x]){
            if(arr[x][y].email){
                if(arr[x][y] === email){
                    _t++;
                }
            }
        }
    }
    return _t;
}

Usage:

if(ifEmailExist(arr,"info@webpack.com") > 0){
   //do stuff
}
RJB
  • 1
  • 1
0

You should use child_added instead of value. Whenever a new node is added in database, child_added will trigger and then you can take action on the data.

var dbRef = firebase.database().ref().child('aanmeldingen');
dbRef.on('child_added', snapshot => {
  var username = snapshot.val().username;
  var email = snapshot.val().email;

  console.log(username);
  console.log(email);
});
gegobyte
  • 4,945
  • 10
  • 46
  • 76
  • Hmm maybe my question was not clear enough. What I would like is to retrieve all data from firebase to check if the inputed value is already inside the database. And if so, do not submit the data. – Giesburts Sep 10 '17 at 17:49
  • I've made some edits to the description to clear out what I am trying to achieve. – Giesburts Sep 10 '17 at 17:54
  • @Gijsberts You don't even need JS code to check for unique values, for that you can use Firebase Database Security Rules. Check out [this](https://stackoverflow.com/a/20291163/4152581) and [this](https://stackoverflow.com/a/35244705/4152581) . – gegobyte Sep 11 '17 at 04:28