How to verify that the node does not exist firebase?

1

How can I check if the name exists in the users database?

With this code I can see that the user exists but when the variable user changes to another user I do not get any error.

var user = 'Moises';
firebase.database().ref('usuarios').orderByChild('usuario').equalTo(user).on('child_added', function(data) {
  console.log('user exist');
}, function(error) {
  console.log(error);
});

@Marcos Solution:

firebase.database().ref('usuarios').orderByChild('usuario').equalTo(user).once('value').then(function(snapshot) {
  console.log(snapshot.exists() ? 'user exist' : 'user non existent');
});
    
asked by jsstoni 21.12.2017 в 16:13
source

1 answer

0

The data parameter is a DataSnapshot , so if you would like to validate if the same does not exist , you should use exists

Example:

var user = 'Moises';
firebase.database()
 .ref('usuarios')
 .orderByChild('usuario')
 .equalTo(user)
 .on('value', function(snapshot) {
    console.log(snapshot.exists() ? 'user exist' : 'user non existent');
   }, function(error) {
     console.log(error);
   });
    
answered by 21.12.2017 в 16:39